0

Hi everyone i try to use JSON.NET with my json files. I want a collection of objects to iterate through and take the information i want.

Here's my json files [EDIT FOR EXPLANATION] I have a collection of launchers (3 here but i will add more later with the same attributs). I need to be able to iterate through all of them and know if they are unlock or not. If they are unlock i'll need to take their attribut for some calcul.

{ "Canon":{ "Unlock": true, "FireRate": 0, "IsRotate": false, "RotateSpeed": 0, "NumberOfBullets": 0 }, "Rotator":{ "Unlock": false, "FireRate": 0, "IsRotate": true, "RotateSpeed": 0, "NumberOfBullets": 0 }, "Back":{ "Unlock": false, "FireRate": 0, "IsRotate": false, "RotateSpeed": 0, "NumberOfBullets": 0 } } 

Here's my classes for mapping

 [System.Serializable] public class Canon { public bool Unlock { get; set; } public int FireRate { get; set; } public bool IsRotate { get; set; } public int RotateSpeed { get; set; } public int NumberOfBullets { get; set; } } [System.Serializable] public class Rotator { public bool Unlock { get; set; } public int FireRate { get; set; } public bool IsRotate { get; set; } public int RotateSpeed { get; set; } public int NumberOfBullets { get; set; } } [System.Serializable] public class Back { public bool Unlock { get; set; } public int FireRate { get; set; } public bool IsRotate { get; set; } public int RotateSpeed { get; set; } public int NumberOfBullets { get; set; } } [System.Serializable] public class Launchers { public Canon Canon { get; set; } public Rotator Rotator { get; set; } public Back Back { get; set; } } 

I have tried to use JObject, JArray and so on but can't find the proper way to be able to do something like that (which, in this case, doesn't work at all :( ).

 void Start() { List<Launchers> jsonList = ReadJson(Application.dataPath + "/Resources/BDD/Launchers.json"); if(jsonList != null){ foreach (var item in jsonList) { if(item.Unlock){ // Do something } } } } static List<Launchers> ReadJson(string pathToJsonFile){ if(File.Exists(pathToJsonFile)){ JObject myjsonfile = JObject.Parse(File.ReadAllText(pathToJsonFile)); List<Launchers> launcherList = JsonConvert.DeserializeObject<List<Launchers>>(myjsonfile .ToString()); return launcherList; }else{ return null; } } 

Sorry for the long post but if someone have some hints/solution, it would be greattly appreciate

10
  • 2
    Your Json represents a single object, not a list Commented Feb 21, 2022 at 11:24
  • @stuartd so my json file is the problem ? I have write it wrong ? Commented Feb 21, 2022 at 11:27
  • 1
    Well we don't know whether you want to represent multiple Launchers instance or a single instance. But if you want to represent multiple launchers as a list, your JSON should use JSON array syntax: [ element1, element2 ] etc Commented Feb 21, 2022 at 11:28
  • 1
    @Rhend he is saying JsonConvert.DeserializeObject<Launchers>(...) instead of JsonConvert.DeserializeObject<List<Launchers>>(...) Commented Feb 21, 2022 at 11:28
  • 1
    Separately, you could probably have a single class (which doesn't need the Serializable attribute btw) called Launcher with those 5 properties, then deserialize to a Dictionary<string, Launcher>. It depends on whether the names Canon, Rotator and Back are really fixed or whether they're just the names that happen to be present. (We really don't have much context here.) Commented Feb 21, 2022 at 11:29

1 Answer 1

1

Okay thanks to @jonSkeet i have made some changes and now everything work as i want ! Here's what i have change to help someone with the same problem as me.

First of all a have change my json file to reflect what i really want (e.g. : A true list of launchers) :

{ "Launchers": [{ "Name": "Canon", "Unlock": true, "FireRate": 0, "IsRotate": false, "RotateSpeed": 0, "NumberOfBullets": 0 }, { "Name": "Rotator", "Unlock": false, "FireRate": 0, "IsRotate": true, "RotateSpeed": 0, "NumberOfBullets": 0 }, { "Name": "Back", "Unlock": false, "FireRate": 0, "IsRotate": false, "RotateSpeed": 0, "NumberOfBullets": 0 } ] } 

Then i have change my mapping to this :

 public class Launcher { public string Name { get; set; } public bool Unlock { get; set; } public int FireRate { get; set; } public bool IsRotate { get; set; } public int RotateSpeed { get; set; } public int NumberOfBullets { get; set; } } public class Listlaunchers { public IList<Launcher> Launchers { get; set; } } 

And finally my last bit of code to use this as i want :

 void Start() { Listlaunchers jsonList = ReadJson(Application.dataPath + "/Resources/BDD/Launchers.json"); if(jsonList != null){ foreach (var item in jsonList.Launchers) { Debug.Log("LAUNCHER JSON => " + item.Name + " => " + item.Unlock); } } } static Listlaunchers ReadJson(string pathToJsonFile){ if(File.Exists(pathToJsonFile)){ JObject objectJson = JObject.Parse(File.ReadAllText(pathToJsonFile)); Listlaunchers launcherList = JsonConvert.DeserializeObject<Listlaunchers>(objectJson.ToString()); return launcherList; }else{ return null; } } 

Thanks for helping me !

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.