1

I'm not sure why below return null this is my json string. It is cause by the JSON string format?

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');
}
2
  • 2
    is that a valid Json ..? Commented Oct 17, 2013 at 7:48
  • 1
    When you format the string correctly (ie remove the returns) the code works fine in a fiddle: jsfiddle.net/RoryMcCrossan/A7aBY Commented Oct 17, 2013 at 7:52

3 Answers 3

2

Remove the line breaks or escape by a \ from json then try like,

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

Sign up to request clarification or add additional context in comments.

Comments

0

It's your line breaks in the string that's not valid JSON. Remove them and it will work fine (or escape them with \).

Comments

0

here you go..

http://jsfiddle.net/AfnX3/

The issue is in your data you cannot have string formatted in such way at least in 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');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.