Json.parse返回null

时间:2013-10-17 07:46:12

标签: jquery json

我不确定为什么以下返回null这是我的json字符串。 它是由JSON字符串格式引起的?

var data = '{"pvd":
            {
            "plannerViewDetailSummary":{"intTargetedCard":6549,"intDeliveredOffer":1,"intRedeemedOffer":1,"dtBegin":"\/Date(1380556800000)\/","dtEnd":"\/Date(1383148800000)\/"},
            "plannerViewDetailChannel":[{"nvarDesc":"Email"}],
            "plannerViewDetailTargetCust":[{"varGroupName":"ALL"}]
            }
    }'

var obj = jQuery.parseJSON(data);
if (obj == null) {
    alert('null');
}

3 个答案:

答案 0 :(得分:2)

json 中移除换行符或从\转义然后尝试,

var data =  '{"pvd":{"plannerViewDetailSummary":{"intTargetedCard":6549,"intDeliveredOffer":1,"intRedeemedOffer":1,"dtBegin":"\/Date(1380556800000)\/","dtEnd":"\/Date(1383148800000)\/"},"plannerViewDetailChannel":[{"nvarDesc":"Email"}],  "plannerViewDetailTargetCust":[{"varGroupName":"ALL"}]}}';
var obj = jQuery.parseJSON(data);
if (obj==null){
   alert('null');
}
console.log(obj);

Fiddle

答案 1 :(得分:0)

这是你在无效JSON字符串中的换行符。删除它们,它将正常工作(或使用\转义它们。)

答案 2 :(得分:0)

在这里你去..

http://jsfiddle.net/AfnX3/

问题出在你的data中,至少在javascript中你不能以这种方式格式化字符串

var data = '{"pvd":{"plannerViewDetailSummary":{"intTargetedCard":6549,"intDeliveredOffer":1,"intRedeemedOffer":2,"dtBegin":"\/Date(1380556800000)\/","dtEnd":"\/Date(1383148800000)\/"},"plannerViewDetailChannel":[{"nvarDesc":"Email"}],"plannerViewDetailTargetCust":[{"varGroupName":"ALL"}]}}'

var obj = jQuery.parseJSON(data);
if (obj == null) {
    alert('null');
} else {
    alert('ok');
}