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\": \"test@test.com\", \"receiver
\": \"test@test.com\"}, {\"message\": \"This is a second test message\", \"sender\": \"test@test.com
\", \"receiver\": \"test@test.com\"}, {\"message\": \"This is a third test message\", \"sender\": \"test
@test.com\", \"receiver\": \"test@test.com\"}]", "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\": \"test@test.com\", \"receiver
\": \"test@test.com\"}, {\"message\": \"This is a second test message\", \"sender\": \"test@test.com
\", \"receiver\": \"test@test.com\"}, {\"message\": \"This is a third test message\", \"sender\": \"test
@test.com\", \"receiver\": \"test@test.com\"}]"
Community
  • 1
  • 1
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • define 'having issues'? I assume you're parsing first with `JSON.parse()` before iterating over the data? – shennan Oct 15 '15 at 16:16
  • Yes the response is parsed with JSON.parse() but still the first branch in the if is taken. – Sebi Oct 15 '15 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. – Álvaro González Oct 15 '15 at 16:22
  • Removed the json tag. – Sebi Oct 15 '15 at 16:27

1 Answers1

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!!!!!)