3
'[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]'

When I try to access the above like this:

for (var i = 0; i < data.length; i++) {
    alert(data[i]);
}

It spells out each thing, such as [, {, ", S, and etc.

I also tried doing data[i].SponsorName but obviously got undefined. How should I be accessing this?

JAAulde
  • 19,250
  • 5
  • 52
  • 63
slandau
  • 23,528
  • 42
  • 122
  • 184

5 Answers5

8

You need to parse the JSON string, preferably with JSON.parse. The JSON API is built into more modern browsers and can be provided to older browsers by including Crockford's JSON script. Crockford's script will detect if the browser already provides the API and adds it if not.

With that in place, if your JSON is in a string variable named response, you can:

var parsedResponse = JSON.parse( response );
//run your iterating code on parsedResponse
JAAulde
  • 19,250
  • 5
  • 52
  • 63
2

You would first need to eval() or more ideally JSON.parse() the JSON string in to a Javascript object. This assumes you trust the source of the JSON.

var jsonobj = JSON.parse(data); 
// Now view the object's structure
console.dir(jsonobj);

Here's what it looks like after being evaluated and printed out:

Screen capture of the JSON obj

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
1
var array = JSON.parse('[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]')
array[0].AccountingManager; // "me"

Or everyone's favorite library, since IE7 and below don't have native support:

$.parseJSON('[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]')
Joe
  • 80,724
  • 18
  • 127
  • 145
  • Whether using jQuery or not, it is best to provide the JSON API to browsers which do not have it. I always include Crockford's lib before including jQuery so that jQuery can always use the JSON API and never resort to eval or other tricks. – JAAulde Oct 25 '11 at 18:07
0

You parsed the Json string first, right?

var data = '[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]';
data = JSON.parse(data);
alert(data.SponsorName);

JSON.parse, when available, is the preferred method over "eval" because of security and performance issues.

Roman
  • 5,888
  • 26
  • 47
0

You've got a JSON array followed by an object:

var data = [{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}];

alert(data[0].SponsorID);
smoak
  • 14,554
  • 6
  • 33
  • 33
  • Based on the output as the OP iterates what he says is his JSON, it is clear that he is working with unparsed JSON, also known as a string. Furthermore, there is no such thing as a "JSON array" or "JSON object". JSON is a serialization scheme which uses a subset of JavaScript literal syntax to describe data structures. So `[1, 2, 3]` is not a JSON array, it is a JavaScript array literal, whereas `'[1, 2, 3]'` is a string of JSON serialized data which can be parsed into an array. – JAAulde Oct 25 '11 at 18:04
  • 1
    From json.org "The JSON array is an ordered collection of values separated by commas. The whole thing is wrapped in square brackets. It maps directly onto arrays, vectors, and lists." Also it states: "A JSON object is an unordered collection of key/value pairs." – smoak Oct 25 '11 at 19:58
  • Ok, so the "no such thing" part of my response is inaccurate based on that description. However, `[]` is still not a JSON array as described in your answer about the code you show. My primary point being that it is important to distinguish between the serialized state and what it could be parsed to depending on the lang/env which parses. – JAAulde Oct 25 '11 at 20:15
  • I think I see what you mean now. Thanks for clarifying it for me. – smoak Oct 25 '11 at 21:59