2013-02-07 29 views
2

我想在js中構建JSON對象。在堆棧溢出本身中已經有關於這個主題的幾篇文章。簡稱How do i build JSON dynamically in javascript?。必須構建與帖子中提到的完全相同的JSON。在JavaScript中構建JSON

{ 
    "privilege": { 
     "accesstype": "VIEW", 
     "attribute": [ 
      { 
       "code": "contenttype", 
       "displayname": "Content type", 
       "value": { 
        "valcode": "book_article", 
        "valdisplayName": "Book Article" 
       } 
      }, 
      { 
       "code": "mime", 
       "displayname": "Mime type", 
       "value": { 
        "valcode": "xml", 
        "valdisplayName": "Xml" 
       } 
      } 
     ] 
    } 
} 

Follwed答案在後,並試圖此,

var privilege = {}; 

privilege.attribute[0].code = "contenttype"; 
privilege.attribute[0].displayname = "Content type"; 
privilege.attribute[0].value.valcode = "book_article"; 
privilege.attribute[0].value.valdisplayName = "Book Article"; 

但得到錯誤的privilege.attribute不確定。

我無法弄清楚我要出錯的地方。假設必須有一些聲明問題。任何燈光都會有很大的幫助。

+0

HTTP:// WWW。 json.org/js.html – mdn

+3

在將成員添加到它之前,您是否嘗試過'privilege.attribute = new Array()'或類似的東西?我認爲這與'attribute'本身沒有被聲明並且你立即嘗試將它用作數組有關。只是一個猜測。 – jonhopkins

+0

那麼,你必須將'privilege.attribute'初始化爲數組,並將該數組的第一個元素作爲對象進行初始化。請查看https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects。一旦你建立了這個對象,你可以使用'JSON.stringify'將它轉換爲JSON。 –

回答

2

試試這個:

var privilege = {}; 
privilege.attribute = []; 
privilege.attribute[0] = {}; 
privilege.attribute[0].code="contenttype"; 
privilege.attribute[0].displayname="Content type"; 
privilege.attribute[0].value = {}; 
privilege.attribute[0].value.valcode="book_article"; 
privilege.attribute[0].value.valdisplayName="Book Article"; 

看一看JavaScript對象。那裏有很多東西。例如http://mckoss.com/jscript/object.htm

編輯:初始化後提示privilege.attribute[0] = {};

+1

但仍然不正確。 'privilege.attribute [0]'也必須被初始化。 –

+0

@FelixKling:謝謝... – devOp

0

試試這個

privilege.attribute = []; 
privilege.attribute.push({}); 

privilege.attribute[0].code="contenttype"; 
privilege.attribute[0].displayname="Content type"; 
privilege.attribute[0].value.valcode="book_article"; 
privilege.attribute[0].value.valdisplayName="Book Article"; 
+0

'privilege.attribute [0]'也必須被初始化。 –

+0

@FelixKling謝謝指出。編輯答案.. –

+0

@Салман感謝您的快速響應。 privilege.attribute [0] .value也必須被初始化。所以做了這樣的事情,privilege.attribute [0] .value = {}; – NitZRobotKoder

1

爲什麼不只是做

var privilege = { 
    "accesstype": "VIEW", 
    "attribute": [{ 
     "code": "contenttype", 
     "displayname": "Content type", 
     "value": { 
      "valcode": "book_article", 
      "valdisplayName": "Book Article" 
     } 
    }, { 
     "code": "mime", 
     "displayname": "Mime type", 
     "value": { 
      "valcode": "xml", 
      "valdisplayName": "Xml" 
     } 
    }] 
} 

...其實,你並不需要的鑰匙是字符串,你可以寫......

var privilege = { 
    accesstype: "VIEW", 
    attribute: [{ 
     code: "contenttype", 
     displayname: "Content type", 
     value: { 
      valcode: "book_article", 
      valdisplayName: "Book Article" 
     } 
    }, { 
     code: "mime", 
     displayname: "Mime type", 
     value: { 
      valcode: "xml", 
      valdisplayName: "Xml" 
     } 
    }] 
} 
+0

所有的值都是動態派生的... – NitZRobotKoder