我不确定为什么以下返回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');
}
答案 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);
答案 1 :(得分:0)
这是你在无效JSON字符串中的换行符。删除它们,它将正常工作(或使用\
转义它们。)
答案 2 :(得分:0)
在这里你去..
问题出在你的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');
}