var JSONObj='[{"rmaNumber":null,"orderReferenceNumber":"referene45","orderStatus":"Pending","dateRequested":null,"dateApproved":null,"segateAddress":null,"billingAddress":null,"shippingAddress":null,"returnForCredit":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"returnForExchange":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"totals":null}]'
var parsedJSON = eval('(\'+JSONObj+\')');
var result=parsedJSON.result;
var count=parsedJSON.count;
alert('result:'+result+' count:'+count);
在警告中给出undefined:undefined
答案 0 :(得分:1)
您应该使用JSON.parse()
:
var parsedJSON = JSON.parse(JSONObj);
console.log(parsedJSON[0].orderStatus); // "Pending"
注意:此方法仅受现代浏览器支持。了解浏览器兼容性here。
答案 1 :(得分:1)
var JSONObj='[{"rmaNumber":null,"orderReferenceNumber":"referene45","orderStatus":"Pending","dateRequested":null,"dateApproved":null,"segateAddress":null,"billingAddress":null,"shippingAddress":null,"returnForCredit":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"returnForExchange":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"totals":null}]';
var parsedJSON = JSON.parse(JSONObj);
var result=parsedJSON.result;
var count=parsedJSON.count;
alert('result:'+result+' count:'+count);
但是,您没有任何名为“result”或“count”的JSON字段,因此这些字段将是未定义的。
答案 2 :(得分:0)
您可以改为使用JSON.parse。
对于不支持JSON.parse
nativly(MSIE)的浏览器,您可以使用第三方库,例如json2或json3。
var parsedJSON = JSON.parse(JSONObj);
答案 3 :(得分:0)
你有引用问题。
您可以通过替换
来解决此问题var parsedJSON = eval('(\'+JSONObj+\')');
与
var parsedJSON = eval('('+JSONObj+')');
但这里没有理由不使用JSON.parse。
答案 4 :(得分:0)
var parsedJSON = eval(JSONObj);
这将为您提供已解析的JSON对象。但是在Json对象中没有属性名result
,count
也是如此。如果您想计算数组,那么您只需使用length
来代替parsedJSON.length
答案 5 :(得分:0)
试
var JSONObj='[{"rmaNumber":null,"orderReferenceNumber":"referene45","orderStatus":"Pending","dateRequested":null,"dateApproved":null,"segateAddress":null,"billingAddress":null,"shippingAddress":null,"returnForCredit":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"returnForExchange":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"totals":null}]';
eval("var parsedJSON =" + JSONObj);
答案 6 :(得分:0)
第一件事:
var parsedJSON = eval('(\'+JSONObj+\')');
应该是
var parsedJSON = eval('('+JSONObj+')');
第二件事:
我没有看到任何result
或count
对象是json字符串
第三件事:
json中的数据在数组中,所以如果对象中存在任何对象result
,则在数组中,应该使用{{1}访问它}