Javascript 如何浏览此JSON对象
我有这个JSON对象,我想用javascript或jquery来导航它,我该怎么做呢Javascript 如何浏览此JSON对象,javascript,jquery,json,Javascript,Jquery,Json,我有这个JSON对象,我想用javascript或jquery来导航它,我该怎么做呢 { "Fporder": { "cep_id": "300000007", "user_id": "300000192", "fporder_type_id": 2, "agency_id": null, "fporder_code": 1, "id": "66" }, "FpordersPr
{
"Fporder": {
"cep_id": "300000007",
"user_id": "300000192",
"fporder_type_id": 2,
"agency_id": null,
"fporder_code": 1,
"id": "66"
},
"FpordersProduct": [
{
"product_id": "1938",
"requested": "4",
"price": 3965,
"product_name": "PLANCHA DE LASAGNA PRE COCIDO X KL",
"product_code": "22044001"
}
]
}
编辑:
请注意,我遇到了以下错误:
未捕获的TypeError:无法读取未定义的属性“product_id”
我尝试过这些方法,但没有成功:
json_object.FpordersProduct.product_id
或
或
但它不起作用。请帮忙
编辑2
函数agregar\u fporder(fporder)
{
log('agregar_fporder()ejecutado');
控制台日志(“fporder:+fporder”);
var tr='';
tr+='';
tr+='';
tr+=''+fporder.FpordersProduct.product_id+'';
tr+=''+fporder.FpordersProduct.product_代码+'';
tr+=''+fporder.FpordersProduct.product_name+'';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
tr+='';
var item=$('table#items tr.items.fporder').find('td.code input.code[value=“”+fporder.FpordersProduct.product_code+”);
console.log('item:'+item);
}
嗯,“浏览它”是一个非常模糊的概念。但是,它是一个对象,所以如果您只需要访问它的属性,就可以像其他任何对象一样访问它
示例:
var some_json_object = {
"Fporder": {
"cep_id": "300000007",
"user_id": "300000192",
"fporder_type_id": 2,
"agency_id": null,
"fporder_code": 1,
"id": "66"
},
"FpordersProduct": [
{
"product_id": "1938",
"requested": "4",
"price": 3965,
"product_name": "PLANCHA DE LASAGNA PRE COCIDO X KL",
"product_code": "22044001"
}
]
}
some_json_object.Fporder // to access Fporder property on root of object
some_json_object.Fporder.cep_id // to access cep_id property on the object assigned to the Fporder property
some_json_object.FpordersProduct[0] // to access the first (currently only) object on the array assigned to the FpordersProduct property
// ... etc.
看到这一点,您也可以看到这一点,以及我是如何通过某个\u json\u object.FpordersProduct[0]的第一个元素进行访问的,因为我正在获取某个\u json\u object.FpordersProduct[0]并在这样做时出错.product_id请显示您在问题中使用的确切代码,包括您的
some_json_object
变量的声明。我将该json对象作为jquery Ajax的响应收到,这很好。因此,请显示您用于处理该响应的代码。您刚才添加的代码段引用了fporder
第meter.您在尝试json\u object.FpordersProduct.product\u id
时说它不起作用。json\u object
在哪里定义?
json_object.FpordersProduct[0]['product_id']
function agregar_fporder(fporder)
{
console.log('agregar_fporder() ejecutado');
console.log("fporder: "+fporder);
var tr = '';
tr += '<tr class="item fporder">';
tr += '<td class="nro_orden"></td>';
tr += '<td class="id">'+fporder.FpordersProduct.product_id+'</td>';
tr += '<td class="code">'+fporder.FpordersProduct.product_code+'</td>';
tr += '<td class="name">'+fporder.FpordersProduct.product_name+'</td>';
tr += '<td></td>';
tr += '<td></td>';
tr += '<td></td>';
tr += '<td></td>';
tr += '<td></td>';
tr += '<td class="nro_pedido">';
tr += '</td>';
tr += '<td class="quantity">';
tr += '</td>';
tr += '<td class="um_id">';
tr += '</td>';
tr += '<td></td>';
tr += '<td>';
tr += '</td>';
tr += '<td></td>';
tr += '<td></td>';
tr += '</tr>';
var item = $('table#items tr.items.fporder').find('td.code input.code[value="'+fporder.FpordersProduct.product_code+'"');
console.log('item: '+item);
}
var some_json_object = {
"Fporder": {
"cep_id": "300000007",
"user_id": "300000192",
"fporder_type_id": 2,
"agency_id": null,
"fporder_code": 1,
"id": "66"
},
"FpordersProduct": [
{
"product_id": "1938",
"requested": "4",
"price": 3965,
"product_name": "PLANCHA DE LASAGNA PRE COCIDO X KL",
"product_code": "22044001"
}
]
}
some_json_object.Fporder // to access Fporder property on root of object
some_json_object.Fporder.cep_id // to access cep_id property on the object assigned to the Fporder property
some_json_object.FpordersProduct[0] // to access the first (currently only) object on the array assigned to the FpordersProduct property
// ... etc.