parsing a json array

I don’t get why this is so stupidly difficult, so please help me.

I have JSON data I need to parse in UnityScript. In PHP or jQuery, this would be a 3-line problem:

$data = json_decode($json);
foreach ($data->features as $feature) {
	echo $feature->properties->name."\n";
}

given my data structure like this, how the f*** do I do the equivalent in Unity ???

{"type":"FeatureCollection","features":[
	{"type":"Feature","properties":{"id":122567,"name":"Wexford","population":185},"geometry":{"type":"Point","coordinates":[80776.1,68776.1]}},
	{"type":"Feature","properties":{"id":121119,"name":"Mothotorn","population":631},"geometry":{"type":"Point","coordinates":[28776.1,12776.1]}},
	{"type":"Feature","properties":{"id":121091,"name":"Rensho","population":346},"geometry":{"type":"Point","coordinates":[27445.3,59445.3]}},
	{"type":"Feature","properties":{"id":121121,"name":"Thyberla","population":187},"geometry":{"type":"Point","coordinates":[28776.1,36776.1]}},
	{"type":"Feature","properties":{"id":121102,"name":"Coplucir","population":337},"geometry":{"type":"Point","coordinates":[28045.3,36045.3]}},
	{"type":"Feature","properties":{"id":121737,"name":"Thusojex","population":236},"geometry":{"type":"Point","coordinates":[50976.1,26976.1]}},
	{"type":"Feature","properties":{"id":121164,"name":"Werolar","population":163},"geometry":{"type":"Point","coordinates":[30178.6,66178.6]}}
]}

.NET has the JavaScriptSerializer class, although I have no idea if Unity’s version of Mono supports it.

There are other options out there. MiniJSON is used in a quite a few asset store plugins, so I guess it works fine.
MiniJSON: http://forum.unity3d.com/threads/35484-MiniJSON-script-for-parsing-JSON-data
Json.NET: http://json.codeplex.com/

Thanks, but I’m not looking for a JSON parser. I’ve found half a dozen and tried them all. The parsing part isn’t the issue. The issue is that whatever I get back from them (objects, hashtables, etc.) provides no easy way to access the array of features I’m parsing.

Not sure if I understand your problem. Any of the JSON parsers that use reflection will leave you with an instantiated object that you can then navigate just like any other class.

So, for instance, with JsonFX (the one we happen to use), you pass in a type to the parser:

// Turn the JSON into C# objects
var search = JsonReader.Deserialize(rawJson);

and out pops an object that you can iterate over as you please, so you can, for instance, do this:

foreach (Tweet tweet in search.results) {}

That example, btw, is from https://bitbucket.org/TowerOfBricks/jsonfx-for-unity3d/src/bd8f83a8c1e5/Unity%20Examples/JsonFxDemo.cs

Is this at all what you’re trying to do, or am I misunderstanding? (BTW, in your case it looks like you’re getting back a dictionary within your array - not all solutions support this, but JsonFX at least I know does.)

Yes, I got as far as iteration working and then I ran into the “object does not support slicing” error that has a couple thousand Google hits but none of them (at least as far as Iooked at them) provides a solution.

My problem seems to be that my JSON data isn’t flat. I need to go deeper into it. I iterate over a sub-object (features) and the actual data is inside elements of that again, so I have a tree:

- root
\-- bla
\-- features
    \-- bla
    \-- data I need
    \--- other data I need

Tom,
If you use a parser like JsonFX, the result won’t be flat - it will use reflection to fill out an instance of a class for you. If you’d like, you can define a class with dictionaries, lists, subclasses, etc.

For an example of what you can parse, here’s what our backend search returns in JSON:

{
“hits”:
[
{
“title”: “US National Weather Service - NCEP(WMC) NDFD product generated by NCEP/HPC Forecast products : Wind_speed”,
“organization”: “Center US National Weather Service - NCEP(WMC) (7) Subcenter Hydrometeorological Prediction Center (5)”,
“id”: “{4EE4252D-007E-4048-8E12-3EC2D6583E9F}”,
“abstractTxt”: “Best time series, taking the data from the most recent run available. : Wind_speed @ height_above_ground:boxfill style, using the redblue palette”,
“startTime”: “2012-04-08T06:00:00Z”,
“endTime”: “2012-05-17T00:00:00Z”,
“maxHeight”: 865,
“maxWidth”: 2000,
“server”: “motherlode.ucar.edu:8080”,
“period”: “PT6H”,
“status”: “active”,
“links”: {
“dataLink”: “http://motherlode.ucar.edu:8080/thredds/wms/fmrc/NCEP/NDFD/CONUS_5km/NCEP-NDFD-CONUS_5km_best.ncd?service=WMS&version=1.3.0&request=GetCapabilities”,
“websiteLink”: “http://motherlode.ucar.edu:8080”
},
“units”: null,
“boundingVariables”: {
“w”: -130.1427500118414,
“e”: -60.84723747666115,
“s”: 20.16521164926921,
“n”: 50.120763518446985
},
“tags”:
[
“UCAR/UNIDATA”,
“forecast_period”,
],
“layers”:
[
{
“name”: “Wind_speed”,
“styles”:
[
“a”,
“b”,
“c”,
“d”,
“e”,
“f”,
“g”,
“h”,
“i”,
“j”
],
“vDims”:
[
10
],
“units”:
[
]
},
{
“name”: “Wind_direction_from_which_blowing”,
“styles”:
[
“a”,
“b”,
“c”,
“d”,
“e”,
“f”,
“g”,
“h”,
“i”,
“j”
],
“vDims”:
[
10
],
“units”:
[
]
}

[…]

{
“name”: “Total_cloud_cover”,
“styles”:
[
“a”,
“b”,
“c”,
“d”,
“e”,
“f”,
“g”,
“h”,
“i”,
“j”
],
“vDims”:
[
],
“units”:
[
]
}
]
}
],
“styleMap”: {
“a”: “redblue”,
“b”: “alg”,
“c”: “ncview”,
“d”: “greyscale”,
“e”: “alg2”,
“f”: “occam”,
“g”: “rainbow”,
“h”: “sst_36”,
“i”: “occam_pastel-30”,
“j”: “ferret”
},
“unitMap”: {
}
}