0

I'm using LitJSON to load .json data, and I'm running some test code with a foreach loop, but it's not working.

InvalidOperationException: Instance of JsonData is not a dictionary
LitJson.JsonData.EnsureDictionary()

(this error is thrown when the foreach line is run)

    using UnityEngine;
    using LitJson;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;

    public void LoadData() {
        JsonReader reader = new JsonReader("[1, 2, 3, 4, 5]");
        JsonData data = JsonMapper.ToObject(reader);

        foreach (JsonData i in data) {
          Debug.Log("foreach: test");
        }
    }

According to the example in the documentation (Ctrl+F "foreach"; it's near the bottom of the page), I'm pretty sure this code should work (?).

Anyone have any ideas as to why this wouldn't work? Advice would be greatly appreciated!

Edit: The solution is to cast data to an IList object as follows:

foreach (JsonData i in data as IList) {
    Debug.Log("foreach: " + i)
}
Novark
  • 419
  • 4
  • 15
  • Offtopic: I would avoid a library which hasn't been updated since 2014, you have Json.Net which is the de-facto standard library for Json handling on .net, also you can use ServiceStack serializers for better performance. – Gusman May 08 '16 at 21:58
  • @Gusman Understood - I've been looking into using a different library that's been updated more recently. Unfortunately, it looks like Json.Net for Unity is paid-only, however there appears to be a few other free options that look promising as well. – Novark May 08 '16 at 22:11
  • Hmmm, not sure on how unity editor works, but if you can reference a .net assembly then just download the regular libraries and use them. Also, you can just go to the github of Json .net, download the code and add it to your project, it's licensed as MIT so you will have no problem at all. – Gusman May 08 '16 at 22:36
  • I might give that a go at some point, however, I've read that Unity has some quirks with using standard .NET features, so I'm not expecting much in that sense. Regardless, I was able to solve my initial issue (solution posted above). Thanks for the help! – Novark May 08 '16 at 23:45
  • The only problem is (if I recall it right) it must be PCL compatible, which Json .net is. – Gusman May 08 '16 at 23:46

1 Answers1

2

1.Don't use foreach on anything that is not array in Unity. It allocates memory mercilessly.

2.Unity has a Built in Json serialize called JsonUtility. I suggest you use that. It currently does not support array and your question suggests you are using Json array. There is a solution to get it work with array. Look here. Read from where it says 2. MULTIPLE DATA(ARRAY JSON). Once you get it working, it will be easy to do next time and no external library required. I also recommend it because it faster than most other Json libraries.

Note: You need Unity 5.3 and above to get this to work.

EDIT:

From my comment, this worked

foreach (var i in data as IList){

}
Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • My data is actually somewhat complex, and contains nested dictionaries / arrays and so forth. The built-in JsonUtility in Unity appears to only handle flat structures. I was hoping to use a library that "just works" without having to go and implement a bunch of hacky workarounds. I'll also try to refrain from using foreach when possible. – Novark May 08 '16 at 22:29
  • @Novark It does have limits but supports nested arrays. Not sure about dictionary. Why not use for loops? – Programmer May 08 '16 at 22:33
  • @Novark I don't really think you have a problem if for loop works. Before I end this, can you add your original code in a try catch block and see if it throw any error. Just copy the code here `try { foreach (JsonData i in data) { Debug.Log("foreach: " + i); } } catch (System.Exception e) { Debug.Log("Error: " + e.Message); }` – Programmer May 08 '16 at 23:05
  • I ran your code snippet out of curiosity, and it hits the catch, and logs the same error as in my original post. I've already verified that for-loops will work, however, so I'll probably just go that route. – Novark May 08 '16 at 23:13
  • @Novark My bad didn't even see your error in your first post. I found the problem and the problem is from the people that made the library. Just in-case you want to know why go here http://stackoverflow.com/questions/33258145/resharper-thinks-that-typecast-is-redundant-but-without-typecast-the-code-does Also it looks like this may work `foreach (var i in data as IList){}` I give up if it doesn't. Just stick with the for loop it doesn't work. Happy coding! – Programmer May 08 '16 at 23:21
  • 1
    Yep! Casting data as IList solved the issue. Thanks for the help! – Novark May 08 '16 at 23:28