0

I would like to know how to pull the 2D array out of the below JSON file. I am using Unity and would ideally like to use Newtonsoft.Json

{ "height":8,
 "infinite":false,
 "layers":[
        {
         "data":[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
         "height":8,
         "name":"Tile Layer 1",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":8,
         "x":0,
         "y":0
        }],
 "nextobjectid":1,
 "orientation":"orthogonal",
 "renderorder":"right-down",
 "tiledversion":"1.1.2",
 "tileheight":64,
}

This is not a duplicate question as it deals with the unique case of having a nested array within the JSON file, and in particular I would like to use Newtonsoft.JSON.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Jim
  • 761
  • 3
  • 10
  • 32

2 Answers2

2

If you want to use Newtonsoft (and pretty much any other JSON library), you'll just need to create a class to hold the deserialized object:

public class Layer
{
    public IEnumerable<int> Data {get;set;}
    public int Height {get;set;}
    // ... implement other properties
}

public class MyObject
{
    public int Height {get;set;}
    public bool Infinite {get;set;}
    public IEnumerable<Layer> Layers {get;set;}
    // ... implement other properties
}

Then deserialize the string into your object:

using Newtonsoft.Json.JsonConvert;
....
var myObject = DeserializeObject<MyObject>(jsonString);
foreach (var layer in myObject.Layers)
{
    // do something with each layer, e.g. get the sum of the data
    var sum = layer.data.Sum();
}
Colin Young
  • 3,018
  • 1
  • 22
  • 46
  • Thank you, this was a clear and easy to implement answer – Jim Feb 02 '18 at 15:08
  • @Jim do take note of the linked answer at the top of the question (https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity), regarding performance of the built-in Unity serializer and the fork of the Newtonsoft library. I'm working with the latest Unity and the experimental .Net 4.6 runtime so I don't need to worry about the incompatibilities -- that might not be the case for you, but since you did ask specifically to use that library... (although I'd argue if JSON performance is a problem in your project, you're doing something wrong) – Colin Young Feb 02 '18 at 17:45
0

If Unity supports dynamic keyword which comes with c# 4, then you can just assign it directly with

dynamic obj = JsonConvert.DeserializeObject(jsonData);

You will then access it directly: obj.layers.data

If the included mono framework doesn't support dynamic keyword, what you can do is create a model of the data which is a simple class with all the attributes and assign it in a similar fashion.

YourModel obj = JsonConvert.DeserializeObject<YourModel>(jsonData);
Berkays
  • 364
  • 3
  • 10
  • Do I have to make a model of the data that is basically a mirror of the Json file? Can you help clarify? dynamic keyword is not supported. – Jim Feb 02 '18 at 13:24
  • Yes you will create c# representation of the json. If you use keys as variable names, JsonConverter will automatically deserialize without extra needs. https://www.ideone.com/W8qlsW @Jim – Berkays Feb 02 '18 at 13:34