解析期间Json值出错

时间:2018-02-08 13:40:48

标签: javascript jquery json

我有一个跟随Json并想要打印键和值。

我们正在接收数据

{\"dcFlag\":null,\"code\":null,\"packageQuantity\":null,\"analgesicTax\":false,\"shelfTicketName\":null,\"productType\":null,\"itemSizeValue\":null,\"unit\":null,\"unitPriceRequired\":false,\"unitPriceMeasure\":null,\"unitPriceDisplay\":null,\"itemSizeUOM\":null,\"australiaMadeGrown\":null,\"departmentCat\":null,\"categoryCat\":null,\"commodityCat\":null,\"subCommodityCat\":null,\"selectedUnit\":null,\"toConsumer\":true,\"toRetailer\":true,\"taxName\":\"1\",\"taxRate\":null,\"countryOfOrigin\":null}"

在JSON.parse之后,数据被转换为

{"dcFlag":null,"code":null,"packageQuantity":null,"analgesicTax":false,"shelfTicketName":null,"productType":null,"itemSizeValue":null,"unit":null,"unitPriceRequired":false,"unitPriceMeasure":null,"unitPriceDisplay":null,"itemSizeUOM":null,"australiaMadeGrown":null,"departmentCat":null,"categoryCat":null,"commodityCat":null,"subCommodityCat":null,"selectedUnit":null,"toConsumer":true,"toRetailer":true,"taxName":"1","taxRate":null,"countryOfOrigin":null}

我试过

 var obj = JSON.parse(result);
                    console.log(obj);
                    $.each(obj, function(k, v) {
                        //display the key and value pair
                        console.log(k + ' is ' + v);
                    });

它抛出错误

Uncaught TypeError: Cannot use 'in' operator to search for '455' in 

任何输入? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

不要使用parse,因为result已经过解析。



var obj = {"dcFlag":null,"code":null,"packageQuantity":null,"analgesicTax":false,"shelfTicketName":null,"productType":null,"itemSizeValue":null,"unit":null,"unitPriceRequired":false,"unitPriceMeasure":null,"unitPriceDisplay":null,"itemSizeUOM":null,"australiaMadeGrown":null,"departmentCat":null,"categoryCat":null,"commodityCat":null,"subCommodityCat":null,"selectedUnit":null,"toConsumer":true,"toRetailer":true,"taxName":"1","taxRate":null,"countryOfOrigin":null};
$.each(obj, function(k, v) {
    //display the key and value pair
    console.log(k + ' is ' + v);
});
console.log(obj);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

如果result包含反斜杠,那么它可以正常工作:

&#13;
&#13;
var result = '{\"dcFlag\":null,\"code\":null,\"packageQuantity\":null,\"analgesicTax\":false,\"shelfTicketName\":null,\"productType\":null,\"itemSizeValue\":null,\"unit\":null,\"unitPriceRequired\":false,\"unitPriceMeasure\":null,\"unitPriceDisplay\":null,\"itemSizeUOM\":null,\"australiaMadeGrown\":null,\"departmentCat\":null,\"categoryCat\":null,\"commodityCat\":null,\"subCommodityCat\":null,\"selectedUnit\":null,\"toConsumer\":true,\"toRetailer\":true,\"taxName\":\"1\",\"taxRate\":null,\"countryOfOrigin\":null}"';
debugger;
result=result.slice('0','-1');
  
var obj = JSON.parse(result); 
$.each(obj, function(k, v) { 
//display the key and value pair 
console.log(k + ' is ' + v); 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;