2017-10-05 108 views
-1

我在這裏有一些代碼json。如何在JavaScript中分割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的「描述」值?

+0

你是如何加載json的 – user3080953

+0

你可以將Json分配給一個變量,並將它作爲一個數組中的一個屬性讀取。例如:Json [1]。說明 –

回答

0

它是一個JSON對象數組,因此您可以訪問那些array objects using indexRead more on JavaScript Array

對於您可以使用的屬性名,也可以通過他們循環使用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) 
 

0

老闕puedes hacer ES recorrerlo CON聯合國bucle ejemplo對象屬性:

var jsonData =[ 
 
    {"Nombre":"Manzana","Cantidad":10}, 
 
    {"Nombre":"Pera","Cantidad":20}, 
 
    {"Nombre":"Naranja","Cantidad":30} 
 
    ]; 
 
var Fruta; 
 
for (item = 0; item < jsonData.length; item++) { 
 
    Fruta = jsonData[item].Nombre; 
 
    alert(Fruta);//Imprimo el nombre de la fruta en una alerta; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

+0

歡迎來到SO,請用英文發表您的答案。 –