1

I'm getting JSON from a servlet and turning the responseText into a JSON object by using JSON.parse(). Chrome Developer Tools shows the JSON object as having the data that I want, but when I actually try to access it I just get a bunch of 'undefined's.

Am I not interpreting the data correctly?

Screenshot of Chrome Developer Tools: Debug output of JSON Object

And briefly, my code to output the data:

        for (var i = 0, len = jsonObj.length; i < len; ++i) {
            // Setup the result...
            var resultRow = document.createElement("tr");
            resultsTable.appendChild(resultRow);            
            var result = jsonObj[i];

            // Name
            var coverCell = resultRow.insertCell(0);
            coverCell.innerHTML = result.name;
        }

jsonData as seen in the screenshot is passed into the output function as jsonObj.

6
  • 1
    Instead of result.name, try result['@name']. Does that work? Commented Apr 8, 2013 at 21:43
  • 2
    The Developer screen shows an object called jsonData, but you are looping over an object called jsonObj. Is this just a typo? Commented Apr 8, 2013 at 21:49
  • FYI, even if you get the data as JSON, after you parsed it, you are working with JavaScript objects and arrays and at this point, whatever you are doing has nothing to do with JSON anymore. Make sure you are accessing the data only once you received it, not beforehand. Commented Apr 8, 2013 at 22:08
  • @cdhowie, that did the trick! Thank you. I tried result.@name earlier, but of course that produced a syntax error. Do you know why the servlet output the JSON with the @ character in front of it? Commented Apr 8, 2013 at 23:37
  • Also, I'd like to mark that as the answer, but it's a comment? @cdhowie Commented Apr 9, 2013 at 1:10

2 Answers 2

1

The key you are trying to access seems to have the @ character at the front. Since the @ character is not a valid identifier and therefore you can't use dot-notation, you can retrieve the value by using bracket notation:

coverCell.innerHTML = result['@name'];
Sign up to request clarification or add additional context in comments.

Comments

0

if you are getting json from the server then why are you using json.parse()? you should directly use the data as a json.

JSON.parse() is used to parse string into JSON. i undertand the response from the server is already a JSON which can used directly without further parsing.

and as a way of troubleshooting you can use console.log to print the object.

3 Comments

I think you are confusing some things here. JSON is a textual data-exchange format, just like XML. The response you get from the server is always going to be text (or binary data). You have to parse JSON into native JavaScript objects/arrays in order to work with them (just like you would parse XML into DOM). JSON can only exists in JavaScript as text, i.e. strings.
jquery post response need not be parsed into JSON if the server is returning a JSON. infact the response is an object and not a string. when i tried to parse it using JSON.parse() i get an error. i checked the type and it's object, and not plain string.
Well, if you setup the Ajax request with jQuery properly, then it will parse the received JSON automatically for you. That does not make my comment invalid though. The response, i.e. was the server sends to you via HTTP, is still text. Also, the OP does not mention jQuery at all.

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.