1

I have the following data passed in from a URL. All of the examples I've seen on recursively printing a JSON object deal with an object that is symmetrical e.g. this one. But how would I print the following in a DIV when each element has a specific name? Do I have to manually reference each field?

I am new to JSON, so any help would be appreciated.

var data = {
    {
            "Message": "success",
            "Status": "done",
            "providerResponse": {
            "referenceNumber": "9876542",
                "errorCode": "0",
                "errorMessage": "Approved",
                "accountNum": "XXXXXXXXXXXX0109",
                "expirationDate": "0116",
                "customerName": "MILTON BERLE",
                "customerAddress1": "614 BROADWAY",
                "customerCity": "NEW YORK",
                "customerState": "NY",
                "customerZIP": "01019",
        }
    }
};

1 Answer 1

2

No, you don't. You loop through the keys one by one.

var obj = JSON.parse(jsonString);
for (key in obj) {
    alert(key + " = " + obj[key]);
    // or do other stuff with the keys and values
}
Sign up to request clarification or add additional context in comments.

2 Comments

This data was generated by the GSON library. Is the opening brace before "Message" necessary? I cannot parse it with jsfiddle.
@EdgeCase I don't think it is, what you have is malformatted stuff, not JSON. But my answer still applies and answers what you were asking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.