1

I'm trying to add the contents of a JSON file to a LIST. Normally, I would use a loop to iterate through the objects, but using COUNT will not compile and when I use LENGTH, it counts the number of characters in the string.

Do I need to make it a JSONArray when the object is made, and if so, how do I do that? Or do I need another kind of loop? I'm using version 5.2.3f, the last XP compatible version. Thanks!

Ken Gordon
  • 195
  • 6
  • 20
  • 1
    **"but using COUNT will not compile"** Why not post that code that is not working? Also post an example of the json you want to add to list. Otherwise this question is not complete. – Programmer Jul 27 '16 at 15:45
  • Possible duplicate of [What's the best way to loop through a JSON of sales data to create a graph in Unity?](http://stackoverflow.com/questions/38535239/whats-the-best-way-to-loop-through-a-json-of-sales-data-to-create-a-graph-in-un) – Fattie Jul 27 '16 at 16:05
  • Are you simply looking for .Length rather than .Count ?? never use arrays for any reason, use only List<> – Fattie Jul 27 '16 at 16:05
  • for(int i = 0; i< Equipmentdata.Count; i++) { testEquipmentList.Add(new Equipment(EquipmentObject.GetString("ID"), EquipmentObject.GetString("Description"))); Debug.Log(testEquipmentList[i]); – Ken Gordon Jul 27 '16 at 16:10
  • As mentioned, .Length counts the number of characters in the JSON string, so the loop goes on 67 times(the number of characters in the string). Currently there is only one object in the file. I want to add it to a list, so I can add another set of data, then another, etc. The problem is that .count does not work. Assets/Scripts/TestJSON.cs(129,43): error CS1061: Type `Boomlagoon.JSON.JSONObject' does not contain a definition for `Count' and no extension method `Count' of type `Boomlagoon.JSON.JSONObject' could be found (are you missing a using directive or an assembly reference?) – Ken Gordon Jul 27 '16 at 16:13

1 Answers1

0

You can use MiniJson:

var jsonString = File.ReadAllText(jsonFileName);
var list = Json.Deserialize(jsonString) as List<object>;
giacomelli
  • 7,287
  • 2
  • 27
  • 31
  • I'm using BoomLagoon currently. Is MiniJson compatible with Unity3d? – Ken Gordon Jul 27 '16 at 18:06
  • yes, it's compatible with Unity3d. I've used it in some Unity3d projects, like here: https://github.com/skahal/Buildron/blob/c75c064790335bb1304f190ab048186609a84014/src/Buildron/Assets/Plugins/MiniJSON.cs – giacomelli Jul 27 '16 at 18:50