我有来自PHP Curl jSon的数据。
以下是jSon数据的示例。
以下是数据:
{
"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": ""
}
]
}
]
}
]
}
}
现在我想获得结果数据 - >成本 - >然后,服务从结果中获得成本价值 - >成本 - >成本 - >价值并将其附加在组合框上。
$.each(jsonStr['rajaongkir']['results'], function(i,n){
cou = '<option value="'+n['costs']['description']+'">'+n['costs']['description']+'</option>';
cou = cou + '';
$("#service").append(cou);
});
我试图运行上面的代码并获得undefined
值。
有什么办法可以搞定吗?
答案 0 :(得分:2)
这看起来像一个动态系统,在动态系统中使用固定索引访问它是一个非常糟糕的主意。我可能会误解你的问题,但这是一个非常灵活的解决方案。使用ES6语法的快速映射功能,我得到了这个数据集:
[
[ { service: 'Surat Kilat Khusus', cost: 16500 },
{ service: 'Express Next Day', cost: 22000 }
],
[ { service: 'OKE', cost: 18000 },
{ service: 'REG', cost: 20000 },
{ service: 'YES', cost: 30000 }
],
[ { service: 'SDS', cost: 135000 },
{ service: 'HDS', cost: 49000 },
{ service: 'ONS', cost: 26000 },
{ service: 'REG', cost: 17000 },
{ service: 'ECO', cost: 14000 }
]
]
这是一个二维数组,用于指出每个项目。这是一个组合的盒子&#34;。长度是动态的而不是固定的,这里也可以通过前面的名称将数据集设置为对象:
[ { 'POS_Indonesia_(POS)': [ [Object], [Object] ] },
{ 'Jalur_Nugraha_Ekakurir_(JNE)': [ [Object], [Object], [Object] ] },
{ 'Citra_Van_Titipan_Kilat_(TIKI)': [ [Object], [Object], [Object], [Object], [Object] ] } ]
我还确保从键中删除空格并用下划线替换它们。这样就不需要提供空间来在js中再次获取密钥,跳过整个&#39;字符串密钥&#39;。
我的代码看起来像这样,但请记住,有更有效的方法:
我首先从提供的目标解构数组,你必须与babel或其他一些现代的转换器进行交互才能完成这项工作。我没有检查性能,但它看起来非常干净,并且可以高度配置。
let [...costs] = somedata.rajaongkir.results;
costs.map(obj => {
let [...costs] = obj.costs;
return {
[obj.name.split(' ').join('_')]: costs.map(obj => ({service:obj.service, cost: obj.cost[0].value}))
}
});
答案 1 :(得分:1)
由于“cost”是一个数组,您希望通过索引使用以下方法访问它:
n['costs'][0]['description']