如何獲得'成本'內的'價值'?請幫助我,我剛剛從w3schools學到了JSON。我試過了,但沒有奏效。我的代碼獲得'價值'是在代碼的底部。獲取嵌套JSON
<p id="demo"></p>
<script>
var text='{ "rajaongkir":{
"query":{
"origin":"501",
"destination":"114",
"weight":1700,
"courier":"jne"
},
"status":{
"code":200,
"description":"OK"
},
"origin_details":{
"city_id":"501",
"province_id":"5",
"province":"DI Yogyakarta",
"type":"Kota",
"city_name":"Yogyakarta",
"postal_code":"55000"
},
"destination_details":{
"city_id":"114",
"province_id":"1",
"province":"Bali",
"type":"Kota",
"city_name":"Denpasar",
"postal_code":"80000"
},
"results":[
{
"code":"jne",
"name":"Jalur Nugraha Ekakurir (JNE)",
"costs":[
{
"service":"OKE",
"description":"Ongkos Kirim Ekonomis",
"cost":[
{
"value":38000, //<<<<<< GET THIS VALUE
"etd":"4-5",
"note":""
}
]
},
{
"service":"REG",
"description":"Layanan Reguler",
"cost":[
{
"value":44000,
"etd":"2-3",
"note":""
}
]
},
{
"service":"SPS",
"description":"Super Speed",
"cost":[
{
"value":349000,
"etd":"",
"note":""
}
]
},
{
"service":"YES",
"description":"Yakin Esok Sampai",
"cost":[
{
"value":98000,
"etd":"1-1",
"note":""
}
]
}
]
}
]}}';
obj=JSON.parse(text);
document.getElementById("demo").innerHTML=
obj.rajaongkir.results[0].costs[0].cost[0].value;
</script>
我愛你'審查** w3sch ls' –
但我認爲它已經成爲未經審查-_- –
學會調試,記錄每一步安慰'然後obj'' obj.rajaongkir'。 chrome或firefox中的'console.log'將非常方便。 –