使用jquery解析JSON以获取一些值
Parsing JSON with jquery to get some value
这是我的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": ""
}
]
}
]
}
]
}
}
我的问题是如何获得"代码"(在rajangkir->result[]->code内部)?我试着使用这个jquery来获取所有的值并附加到我的selectbox中。
$.each(response['rajaongkir']['results']['code'], function(i,n){
cou = '<option value="'+n['code']+'">'+n['code']+'</option>';
cou = cou + '';
$(id).append(cou);
});
但是里面出了点问题。。我想从json(pos,jne,tiki)中给我的selectbox 3值
有人能帮我吗?
看起来您正在尝试在JSON中迭代"results"数组。
尝试删除['code']
,例如:
$.each(response['rajaongkir']['results'], function(i,n){
...
相关文章:
- 在for循环中未获取JQuery/JavaScript对象值
- 点击获取jQuery上的href属性
- 如何获取jQuery Confirm(jConfirm)值并使用该值检查条件
- 通过传递的参数位置获取jQuery中图像的宽度
- MVC Syncfusion Grid-HTML EJ Grid-获取Jquery中所选行的主键
- 如何获取 jQuery 以触发本机自定义事件处理程序
- 如何获取 jQuery 回调触发返回值
- 如何在wordpress插件中获取jquery中的img url和文件url
- 我想知道如何只激活表单提交,而不获取jquery中ajax url的值
- 如何获取jQuery集合中的第k个元素..作为JQUERY元素
- 从aspx.cs页面获取jquery post中的Null响应而不是json
- 如何获取 jquery 变量 childeren
- 如何从每个对象获取jQuery的“title”和“url”值
- 如何动态获取jquery noConflict()的引用
- 用于获取jQuery元素或值的方法
- 获取jquery数组对象的第n项
- 获取jquery确认在函数中返回的结果
- 获取JQuery UI自动完成元素的索引
- 获取jQuery UI排序后的ID数组
- 获取 jQuery DataTable 中的元素