0

After reading a number of questions on parsing an object in javascript I'm still having issues with parsing the following query response:

{"messagesProfile": "[{\"message\": \"This is a test message\", \"sender\": \"[email protected]\", \"receiver
\": \"[email protected]\"}, {\"message\": \"This is a second test message\", \"sender\": \"[email protected]
\", \"receiver\": \"[email protected]\"}, {\"message\": \"This is a third test message\", \"sender\": \"test
@test.com\", \"receiver\": \"[email protected]\"}]", "successProfileMessages": true}

The code that parses the above response is:

if(data.successProfileMessages === false) {
            alert("Failed to retrieve messages");
        } else {
            if(typeof data.messagesProfile != "undefined" && data.messagesProfile != null && data.messagesProfile.length > 0) {
                messages = messages + "<tr>";
                messages = messages + "<td>";
                messages = messages + "There are no messages yet!";
                messages = messages + "</td>";
                messages = messages + "<td>";
            } else {
                // Successfully retrieved messages
                for(var i in  data) {
                    messages = messages + "<tr>";
                    messages = messages + "<td>";
                    messages = messages + data.messagesProfile.sender[i];
                    messages = messages + "</td>";
                    messages = messages + "<td>";
                    messages = messages + data.messagesProfile.message[i];
                    messages = messages + "</td>";
                    messages = messages + "</tr>";
                }
            }
        }

How is it possible to unescape the escaped double quotes and iterate through the JSON object's array fields?

"[{\"message\": \"This is a test message\", \"sender\": \"[email protected]\", \"receiver
\": \"[email protected]\"}, {\"message\": \"This is a second test message\", \"sender\": \"[email protected]
\", \"receiver\": \"[email protected]\"}, {\"message\": \"This is a third test message\", \"sender\": \"test
@test.com\", \"receiver\": \"[email protected]\"}]"
4
  • define 'having issues'? I assume you're parsing first with JSON.parse() before iterating over the data? Commented Oct 15, 2015 at 16:16
  • Yes the response is parsed with JSON.parse() but still the first branch in the if is taken. Commented Oct 15, 2015 at 16:18
  • That code assumes your JSON is already parsed. I think your question has nothing to do with JSON in the first place—you probably just want to iterate through a JavaScript data structure. Commented Oct 15, 2015 at 16:22
  • Removed the json tag. Commented Oct 15, 2015 at 16:27

1 Answer 1

2

the messageProfile property of data is a string so you need to parse it

you can do this in the else clause

var arrayResult = JSON.parse(data.messagesProfile);
for (var i = 0, len = arrayResult.length; i < len; i++) {
  var item = arrayResult[i];
  // do your stuff
}

For iterating over an array i discourage the use of for (var i in arrayResult) cause it will not give you the desired result. It will iterate through all the properties of the object (including the length property!!!!!)

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

1 Comment

That did it. I've messed up the if.

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.