0

我还是新手,JSON所以我可能有一个非常愚蠢的问题。我该如何参考
list.data[0].bom-item.descriptor?我创建的警报声明对我不起作用。感谢帮助。

var txt='{"list": {"data": [{"bom-item": {"dmsID": 3949,"bomPath": "3951B3949","workspaceType": 6,"workspaceID": 8,"bomDepthLevel": 1,"quantity": 1,"formattedQuantity": "1.0","descriptor": "500-0059-000 - FCI, P681-C01 SKU77 (900-60681-0077-400) [REV:D]","revision": "D","units": "EA","itemNumber": 1,"assembly": false,"cost": 0,"isPinned": false,"quoteID": 0,"isUsingDefaultQuote": false,"leaf": false,"redlinedCost": -1,"redlineAddition": false,"redlineAgainstVersion": -1,"redlineDeletion": false,"totalWeight": 1,"lifecycleStatus": "Production","hasSourcing": false,"fields": null}}]}}'



alert(txt.list.data[0].bom-item.descriptor);
4

2 回答 2

1

bom-item不是合法的 Javascript 标识符。

相反,您需要使用索引器表示法:

txt.list.data[0]["bom-item"].descriptor

或者,更好的是,使用 camelCase 代替。

于 2013-08-13T14:28:36.867 回答
1

在您当前的代码中,txt它只是一个字符串,因此它没有 JSON 对象属性。您需要使用JSON.parse()将其转换为 JSON 对象(并使用另一个答案中所述的索引表示法——连字符-是造成问题的原因):

<  yourObj = JSON.parse('{"list": {"data": [{"bom-item": {"dmsID": 3949,"bomPath": "3951B3949","workspaceType": 6,"workspaceID": 8,"bomDepthLevel": 1,"quantity": 1,"formattedQuantity": "1.0","descriptor": "500-0059-000 - FCI, P681-C01 SKU77 (900-60681-0077-400) [REV:D]","revision": "D","units": "EA","itemNumber": 1,"assembly": false,"cost": 0,"isPinned": false,"quoteID": 0,"isUsingDefaultQuote": false,"leaf": false,"redlinedCost": -1,"redlineAddition": false,"redlineAgainstVersion": -1,"redlineDeletion": false,"totalWeight": 1,"lifecycleStatus": "Production","hasSourcing": false,"fields": null}}]}}')
>  [object Object]
<  yourObj.list.data[0]["bom-item"].descriptor
>  "500-0059-000 - FCI, P681-C01 SKU77 (900-60681-0077-400) [REV:D]"

MDN上有关该JSON对象的信息。

于 2013-08-13T14:33:29.870 回答