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 = eval('(\'+JSONObj+\')');    
    var result=parsedJSON.result;
    var count=parsedJSON.count;
    alert('result:'+result+' count:'+count);

在警报中给出未定义的:未定义

4

7 回答 7

1

您应该JSON.parse()为此使用:

var parsedJSON = JSON.parse(JSONObj);
console.log(parsedJSON[0].orderStatus);  // "Pending"

注意:此方法仅受现代浏览器支持。在此处阅读有关浏览器兼容性的信息。

于 2013-03-13T08:25:49.503 回答
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 字段,因此这些字段是未定义的。

于 2013-03-13T08:25:53.547 回答
0

您可以改用JSON.parse

对于不支持JSON.parsenativly(MSIE) 的浏览器,您可以使用第三方库,如json2json3

var parsedJSON = JSON.parse(JSONObj); 
于 2013-03-13T08:26:04.847 回答
0

你有报价问题。

您可以通过更换来修复它

var parsedJSON = eval('(\'+JSONObj+\')');    

var parsedJSON = eval('('+JSONObj+')');    

但是这里没有理由不使用JSON.parse

于 2013-03-13T08:26:29.710 回答
0
var parsedJSON = eval(JSONObj);

这将为您提供已解析的 JSON 对象。result但是您的 Json 对象中没有属性名称,因此也没有count。如果你想计算数组,那么你只需使用length例如parsedJSON.length

于 2013-03-13T08:28:57.320 回答
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);
于 2013-03-13T08:31:22.120 回答
0

第一件事:

var parsedJSON = eval('(\'+JSONObj+\')');

应该

var parsedJSON = eval('('+JSONObj+')');   

第二件事:
我没有看到任何resultcount对象是 json 字符串

第三件事:
json中的数据在数组中,因此如果对象中有任何对象result,则在数组中应该使用parsedJSON[0].result

于 2013-03-13T08:50:19.433 回答