获取嵌套的JSON

时间:2015-05-06 12:40:07

标签: javascript json

如何在“成本”中获得“价值”?请帮助我,我刚刚从w3schools学习了JSON。我试过了,但没用。获取'value'的代码位于代码的底部。

 <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>

4 个答案:

答案 0 :(得分:4)

您的输入字符串错误

<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>

答案 1 :(得分:2)

您的JSON不是有效的字符串文字。除非您使用\结束每一行。

,否则不允许换行

如果你添加这些\,一切都很好:

https://jsfiddle.net/ec3n6f20/

答案 2 :(得分:1)

首先将text字符串解析为Json结构

然后它完全取决于each函数和getter的组合:

var input= JSON.parse(text);

input.results.forEach (function(result){
  result.costs.forEach (function(costEntity){
     console.log(costEntity.cost.value);
  });
});

但是在通过Chrome控制台运行代码之后 - 即使是作为字符串也无法编译,所以首先修复:)

答案 3 :(得分:1)

您的代码是正确的但在Javascript中我们无法在多行中声明字符串。

您有两种解决方法:

  1. 只在一行中生成字符串

    \
  2. 在每行末尾添加var text='{ "rajaongkir":{ \ "query":{ \ ... "origin":"501", \ }}}';

    '
  3. +中换行每一行并在开头添加var text='{ "rajaongkir":{ ' + ' "query":{' ... + ']}}';

    Private Sub QshipNum_AfterUpdate()
    
    If Me.IsShip_.Value = True Then Me.QshipNum.Value = "S" + Me.QuoteID.Value
    
    End If
    
    End Sub