Working with LitJson in C# in Unity

2.1k Views Asked by At

I am trying to port a piece of Code from Java to C# and I am stuck in JSon parsing. Have a look at the following Java Code

        mJsonObject = new JSONObject(str);
        Iterator<String> keys=mJsonObject.keys();
        while(keys.hasNext()){

            String key=keys.next();
            String value=mJsonObject.getString(key);

            mAdData.add(new AdData(key, new JSONObject(value)));


        }

I had a string which has verified Json format and I passed it to JSONObject and every thing was finely working in Java, but now in C# Unity I am not able to port it successful. I am using LitJson to perform this task and I have no idea how this works. I am badly stuck please help. Thanks

1

There are 1 best solutions below

3
On

The keys method of the JSONObject class returns an ICollection<string>. You can iterate an ICollection like this. So I would change your while loop into a foreach, like this:

foreach (string key in keys) {
    //whatever
}