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",
        }
    }
};
Community
  • 1
  • 1
EdgeCase
  • 4,719
  • 16
  • 45
  • 73

1 Answers1

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
}
  • This data was generated by the GSON library. Is the opening brace before "Message" necessary? I cannot parse it with jsfiddle. – EdgeCase Mar 27 '13 at 17:19
  • 1
    @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. –  Mar 27 '13 at 17:20