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.
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:
Should be:
(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
professorResponseas an array and access its element 0 withprofessorResponse[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:Note: in your example you initialise
messagesto refer to an empty array, but then you immediately assign it equal to the return fromYAHOO.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.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().