简体   繁体   English

JSON对象带有变量

[英]JSON Object w/variables in it

I need to create a JSON object "bio" which contains a lot of variables in it: 我需要创建一个JSON对象“bio”,其中包含很多变量:

var bio = {
"name": "my name",
"role": "Web Developer",
"contacts":
    {
        "mobile": "my phone",
        "email": "my@email.address",
        "github": "https://github.com/",
        "twitter": "https://twitter.com/",
        "location": "Los Angeles, CA"
    },
"welcomeMessage": "Welcome to my online resume.",
"skills": ["HTML5","CSS3","JavaScript","Bootstrap", "Angular", "CoffeeScript", "W3"],
"biopic": "http://placehold.it/150x150",
"display": displayFunc(){

}

};

When I try to run this with: 当我尝试运行时:

var formattedName = HTMLheaderName.replace('%data%', bio.name);
var formattedRole = HTMLheaderRole.replace('%data%', bio.role);

$('#header').prepend(formattedRole);
$('#header').prepend(formattedName);

Nothing happens. 什么都没发生。 I believe the error is somewhere in the "contacts" variable because if I comment out contacts and everything below it the name and role show up. 我认为错误是在“contacts”变量中的某个地方,因为如果我注释掉联系人及其下面的所有内容,则会显示名称和角色。 But if I comment out welcomeMessage and everything below that I still get nothing. 但是,如果我评论出welcomeMessage以及下面的所有内容,我仍然一无所获。

EDIT: For this class the contact variable is required to be: 编辑:对于这个类,联系人变量必须是:

contacts : an object with
      mobile: string
      email: string 
      github: string
      twitter: string (optional)
      location: string

If you add function before displayFunc(){} then it should work (as @BubbleHacker mentioned) 如果你在displayFunc(){}之前添加function ,那么它应该工作(如提到的@BubbleHacker)

Demo here: https://jsfiddle.net/mrLh3pz7/ 在这里演示: https//jsfiddle.net/mrLh3pz7/

var bio = {
"name": "my name",
"role": "Web Developer",
"contacts": [
    {
        "mobile": "my phone",
        "email": "my@email.address",
        "github": "https://github.com/",
        "twitter": "https://twitter.com/",
        "location": "Los Angeles, CA"
    }
],
"welcomeMessage": "Welcome to my online resume.",
"skills": ["HTML5","CSS3","JavaScript","Bootstrap", "Angular", "CoffeeScript", "W3"],
"biopic": "http://placehold.it/150x150",
"display": function displayFunc(){}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM