如何用javascript分割json中的值?

时间:2017-10-05 17:18:10

标签: javascript arrays json parsing jsonparser

我在这里有一些代码json。

[ 
    {
      "id": "Node-1",
      "label": "Node-1",
      "image": "img/hosts.png",
      "shape": "image",
      "description": "Node-1:type: OS::Nova::Serverproperties: image: {get_param:image} flavor: {get_param:flavor}"
     },
     {
      "id": "Switch-1",
      "label": "Switch-1",
      "image": "img/switch.png",
      "shape": "image",
      "description": "Switch-1:type: OS::Neutron::Netproperties:   name:Switch-1Switch-1_subnet:"
     }
]

我怎样才能得到"描述"使用javascript值?

1 个答案:

答案 0 :(得分:0)

它是一个JSON对象数组,因此您可以访问array objects using indexRead more on JavaScript Array

对于Object属性,您可以使用property属性,也可以使用Object.keys or Object.getOwnPropertyNames()

循环它们



var x=[ 
    {
      "id": "Node-1",
      "label": "Node-1",
      "image": "img/hosts.png",
      "shape": "image",
      "description": "Node-1:type: OS::Nova::Serverproperties: image: {get_param:image} flavor: {get_param:flavor}"
     },
     {
      "id": "Switch-1",
      "label": "Switch-1",
      "image": "img/switch.png",
      "shape": "image",
      "description": "Switch-1:type: OS::Neutron::Netproperties:   name:Switch-1Switch-1_subnet:"
     }
]
 alert(x[0].description)