2016-01-05 66 views
1

這是我的JSONJSON解析與jQuery得到一些價值

{ 
    "rajaongkir": { 
    "query": { 
     "origin": "23", 
     "destination": "152", 
     "weight": 1500, 
     "courier": "all" 
    }, 
    "status": { 
     "code": 200, 
     "description": "OK" 
    }, 
    "origin_details": { 
     "city_id": "23", 
     "province_id": "9", 
     "province": "Jawa Barat", 
     "type": "Kota", 
     "city_name": "Bandung", 
     "postal_code": "40000" 
    }, 
    "destination_details": { 
     "city_id": "152", 
     "province_id": "6", 
     "province": "DKI Jakarta", 
     "type": "Kota", 
     "city_name": "Jakarta Pusat", 
     "postal_code": "10000" 
    }, 
    "results": [ 
     { 
     "code": "pos", 
     "name": "POS Indonesia (POS)", 
     "costs": [ 
      { 
      "service": "Surat Kilat Khusus", 
      "description": "Surat Kilat Khusus", 
      "cost": [ 
       { 
       "value": 16500, 
       "etd": "2-4", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "Express Next Day", 
      "description": "Express Next Day", 
      "cost": [ 
       { 
       "value": 22000, 
       "etd": "1", 
       "note": "" 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "code": "jne", 
     "name": "Jalur Nugraha Ekakurir (JNE)", 
     "costs": [ 
      { 
      "service": "OKE", 
      "description": "Ongkos Kirim Ekonomis", 
      "cost": [ 
       { 
       "value": 18000, 
       "etd": "2-3", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "REG", 
      "description": "Layanan Reguler", 
      "cost": [ 
       { 
       "value": 20000, 
       "etd": "1-2", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "YES", 
      "description": "Yakin Esok Sampai", 
      "cost": [ 
       { 
       "value": 30000, 
       "etd": "1-1", 
       "note": "" 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "code": "tiki", 
     "name": "Citra Van Titipan Kilat (TIKI)", 
     "costs": [ 
      { 
      "service": "SDS", 
      "description": "Same Day Service", 
      "cost": [ 
       { 
       "value": 135000, 
       "etd": "", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "HDS", 
      "description": "Holiday Delivery Service", 
      "cost": [ 
       { 
       "value": 49000, 
       "etd": "", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "ONS", 
      "description": "Over Night Service", 
      "cost": [ 
       { 
       "value": 26000, 
       "etd": "", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "REG", 
      "description": "Regular Service", 
      "cost": [ 
       { 
       "value": 17000, 
       "etd": "", 
       "note": "" 
       } 
      ] 
      }, 
      { 
      "service": "ECO", 
      "description": "Economi Service", 
      "cost": [ 
       { 
       "value": 14000, 
       "etd": "", 
       "note": "" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

而我的問題是我怎麼能獲得「代碼」(rajaongkir-內部>導致[] - >代碼)?我試着用這個jQuery來獲得所有的價值,並追加到我的選擇框。

$.each(response['rajaongkir']['results']['code'], function(i,n){ 
      cou = '<option value="'+n['code']+'">'+n['code']+'</option>'; 
      cou = cou + ''; 
      $(id).append(cou); 
      }); 

但蹊蹺的在那裏..我想給從JSON(POS,JNE,忻)我的選擇框3值

有人能幫助我嗎?

+1

僅供參考,你並不需要所有的方括號和報價。 'response.rajaongkir.results'和'n.code'可以正常工作。在JavaScript(而不是JSON)中,只有當名稱不是有效標識符時才需要引號。 –

回答

3

它看起來像你試圖迭代你的JSON中的「結果」數組。

嘗試例如去除['code']

$.each(response['rajaongkir']['results'], function(i,n){ 

... 
+0

非常感謝你的傢伙!問題解決了! @ mattbell87 –