J W
J W

Reputation: 878

parse JSON with YUI with [] around {} in packet

I'm pretty new to JSON, javascript, YUI and trying to compelte a homework assignment. I have a JSON packet of the form

[{"id":"1234", "name":"some description","description":"url":"www.sd.com"}, {same format as previous one}]

I tried an example with YUI to pase a jsonString like this:

var jsonString = '{"id":"1234", "name":"some description","description":"url":"www.sd.com"}';
var messages = [];
messages = YAHOO.lang.JSON.parse(jsonString);

then I print out the result in messages[]. In the jsonString example, I get my output. When trying to do it from the professor's web server, I get a failure on parsing. I'm assming it's from the way his packet is surrounded by a [] and mine is not in my example. I tried doing

YAHOO.lang.JSON.parse(professorResponse[0]);

and that also returned an error. I was wondering what the best format/practices in this scenario on what to do with something passed back from a web server in terms of how to format the data so I can parse it. I have zero experience in this area and want to get off to a good start. Thanks.

Edit:

To parse the web server's response, I'm doing this:

function sendRequest() {
  var url = "class website&response=JSON";
  var callback = {success:handleResponse, failure:handleFailure, timeout:5000};
  var transaction = YAHOO.util.Connect.asyncRequest("GET", url, callback, null);
}

// this gets called when my handleResponse methods looks if it's JSON vs XML and sees that the server's response is JSON
function parseJSONResponse(response) {
  var messages = [];
  try {
    messages = YAHOO.lang.JSON.parse(response);
  }
  catch (e) {
    alert("JSON parse failed");
    return;
  }
}

I continue to get JSON Parse failed when I try to parse the response.

Upvotes: 0

Views: 2686

Answers (3)

nnnnnn
nnnnnn

Reputation: 150080

Both your example and the professor's are invalid JSON that can't be parsed because both include a "description" property that doesn't have a value:

... "description":"url":"www.sd.com" ...

Should be:

... "description": "somevalue", "url":"www.sd.com" ...

(Or just remove "description":)

The rest of my answer assumes the above can be fixed before continuing...

You don't explain how you are getting your professor's JSON, or how you generate your output, but in a general sense JSON is a string representation of an object or array, that you then parse to create an actual object or array. Your example is a representation of an object. Your professor's example is a representation of an array of objects.

Where you're going wrong is you seem to be trying to treat professorResponse as an array and access its element 0 with professorResponse[0] before you've parsed it, but if it is JSON then it is a string representing an array not an actual array so you need to parse it first:

// get professorResponse from server somehow
var professorResponse = '[{"id":"1234", "name":"some description","description":"fixed","url":"www.sd.com"}, {same format as previous one}]';

var parsedResponse = YAHOO.lang.JSON.parse(professorResponse);

// now parsedResponse is an array of objects, so
parsedResponse.length   // is 2 - there are two elements
parsedResponse[0]       // is first element, i.e., {"id":"1234", "name":"some description","description":"url":"www.sd.com"}
parsedResponse[1]       // is second element
parsedResponse[0].id    // is "1234"
parsedResponse[0].name  // is "some description"

Note: in your example you initialise messages to refer to an empty array, but then you immediately assign it equal to the return from YAHOO.lang.JSON.parse(jsonString) which in this case will not be an array (because your jsonString represents an object that is not an array) - your original empty array is thrown away.

"what to do with something passed back from a web server in terms of how to format the data so I can parse it."

If the web server is returning valid JSON you don't need to format it in order parse it - it will already be a string in a format that can be parsed with JSON.parse().

Upvotes: 1

ctdeveloper
ctdeveloper

Reputation: 290

Well [] is an array and {} an object.

So if you had the following:

var jsonString =='{"id":"1234", "name":"some description","description":"url":"www.sd.com"}';

jsonString.id would return 1234.

So in this example from above:

var someData = [{"id":"1234", "name":"some description","description":"url":"www.sd.com"}, {same format as previous one}]

You would use:

someData[0].id to get the ID of the first object.

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227300

This isn't valid JSON, every key needs a value. You're missing a value for "description"

{"id":"1234", "name":"some description","description":"url":"www.sd.com"}
             //there is no value for "description ---^

It should look something like this:

{"id":"1234", "name":"some description", "description":"testing", "url":"www.sd.com"}

Upvotes: 2

Related Questions