1

我不确定为什么下面返回 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');
}
4

3 回答 3

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);

小提琴

于 2013-10-17T07:50:21.710 回答
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');
}
于 2013-10-17T07:58:37.210 回答
0

字符串中的换行符不是有效的 JSON。删除它们,它会正常工作(或使用 逃避它们\)。

于 2013-10-17T07:51:00.063 回答