1

Challenge: How to read/parse the following json:

> {"d":"{\"NewDataSet\":{\"Table\":{\"LICENSE_PLATE\":\"PLATE
> 1\",\"PARKING_LOCATION\":\"1001\",\"START_TIME\":\"05:05:00\"}}}"}

Tried Approaches:

var objNDS = jQuery.parseJSON(e.d); //where e.d is the above returned json
var objTbl = objNDS.NewDataSet.Table;
var tblARR = [];
$.each(objTbl, function (i, val) {
  tblARR.push([val.LICENSE_PLATE, val.PARKING_LOCATION, val.START_TIME]);
});

Thanks for your help.

1
  • What's the result of your "Tried Approaches" ?????? Commented Oct 31, 2012 at 22:14

2 Answers 2

2

Your Table, as is shown, is not a list. Using $.each will iterate over its key/value pairs, so you'll have:

i                 val
LICENSE_PLATE     PLATE1
PARKING_LOCATION  1001
START_TIME        05:05:00

Probably your input is wrong, and what you mean is actually:

...\"Table\":[{\"LICENSE_PLATE\":...}, {\"LICENSE_PLATE\":...}]...

If the input is correct, and Table is supposed to have only one value, don't use each and query the objTbl directly instead:

tblARR.push([objTbl.LICENSE_PLATE, objTbl.PARKING_LOCATION, objTbl.START_TIME]);

Update: if both forms can show, just test if objTbl is an array and, if not, wrap it (ideally your server should send this data normalized, though). Then your original code will work as it is:

if ( !(objTbl instanceof Array) )
    objTbs = [objTbl];
$.each(objTbl, function(i, val) {
    tblARR.push([val.LICENSE_PLATE, val.PARKING_LOCATION, val.START_TIME]);
});
Sign up to request clarification or add additional context in comments.

2 Comments

It appears that my server returns a json for a single record in the following format: {"d":"{\"NewDataSet\":{\"Table\":{\"LICENSE_PLATE\":\"PLATE 1\",\"PARKING_LOCATION\":\"1001\",\"START_TIME\":\"05:05:00\"}}}"}.... while multiple records would return: {"d":"{\"NewDataSet\":{\"Table\":[{\"LICENSE_PLATE\":\"PLATE 1\",\"PARKING_LOCATION\":\"1001\",\"START_TIME\":\"05:05:00\"},{\"LICENSE_PLATE\":\"PLATE 2\",\"PARKING_LOCATION\":\"1002\",\"START_TIME\":\"10:10:10\"}]}}"}. Thus, what would you recommend as the best way to check for each scenario?
objTbl instanceof Array. Check the updated answer. Just one question: what would happen if there is no record in the returned set? You might want to guard against that scenario also.
0

I tried to interpret what you're attempting to accomplish and put together a JS fiddle implementation. Note that I had to change the format of the "e" variable.

var e = {"d":"{\"NewDataSet\":{\"Table\":[{\"Plate\":{\"LICENSE_PLATE\":\"PLATE 1\",\"PARKING_LOCATION\":\"1001\",\"START_TIME\":\"05:05:00\"}}, {\"Plate\": {\"LICENSE_PLATE\":\"PLATE 1\",\"PARKING_LOCATION\":\"1001\",\"START_TIME\":\"05:05:00\"}}]}}"};

Then you loop with a regular for loop.

Link is: http://jsfiddle.net/Dracorat/EFcjA/10/

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.