簡體   English   中英

JSON對象帶有變量

[英]JSON Object w/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(){

}

};

當我嘗試運行時:

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

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

什么都沒發生。 我認為錯誤是在“contacts”變量中的某個地方,因為如果我注釋掉聯系人及其下面的所有內容,則會顯示名稱和角色。 但是,如果我評論出welcomeMessage以及下面的所有內容,我仍然一無所獲。

編輯:對於這個類,聯系人變量必須是:

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

如果你在displayFunc(){}之前添加function ,那么它應該工作(如提到的@BubbleHacker)

在這里演示: 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