Hi I'm experiencing a bug on iOS, not present on the editor using LitJSON: Code (csharp): var data : JsonData = JsonMapper.ToObject (text); var resultCount = data["resultCount"].ToString(); // Second use ERROR here First time works perfectly, second use, it crashes: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. Help needed I'm really stuck on it
Hi, I searched and found that JsonMapper is not compatible with IOS. I created my own functions to find the needed Values. first of all copy the LitJson.dll file from Paul Tondeur web site, (see links below) to your asset folder Code (csharp): import LitJson; function GetVal (jsonString:String,property:String):String { var reader : JsonReader = new JsonReader(jsonString); while (reader.Read()) { if (reader.Token.ToString() == "PropertyName" reader.Value.ToString() == property ) { reader.Read(); if (reader.Token.ToString() == "Double" || reader.Token.ToString() == "Int" || reader.Token.ToString() == "String") return reader.Value.ToString(); } } } function GetArray(jsonString:String,property:String):Array { var array = new Array (); array.Clear(); var reader : JsonReader = new JsonReader(jsonString); while (reader.Read()) { if (reader.Token.ToString() == "PropertyName" reader.Value.ToString() == property ) { reader.Read(); if (reader.Token.ToString() == "ArrayStart") { while (reader.Token.ToString() != "ArrayEnd") { reader.Read(); if (reader.Token.ToString() == "Double" || reader.Token.ToString() == "Int" || reader.Token.ToString() == "String") { array.Push (reader.Value.ToString()); } } } return array; } } } And these could be used to find the needed values. Code (csharp): var text : String; var resultCount = GetVal (text, "resultCount"); Debug.Log (resultCount); //and with an array or value : var Screenshots = new Array () Screenshots.Clear(); Screenshots = GetArray (text, "screenshotUrls"); for (var i =0; i< Screenshot.length; i++) Debug.Log (Screenshot[i]); Hope it would help people stuck with this JsonMapper issue Thanks to people who shared, found the solution in this thread : http://answers.unity3d.com/questions/52199/how-to-implement-litjson.html and thanks to Paul Tondeur for sharing his LitJson.dll file and his tutorial : http://www.paultondeur.com/2010/03/...al-xml-and-json-files-with-unity-part-2-json/
Hey, This thread has been really helpful so far, thank you! My only question: Am I correct in saying that the code you provided won't handle nested arrays in the JSON very well? Thanks again!
Did you ever figure out why it doesn't work on ios? I am having the exact same problem, but I'd really like to use the JsonMapper and I can't find why it shouldn't work on ios as it doesn't do anything that would be forbidden on ios.
Just as update. If using the ToObject<T> method in C# and providing a Type for the mapper to map the data to it seems to work also on ios.
i use a free json parser thats on the asset store, works great https://www.assetstore.unity3d.com/#/content/5788 very simplistic, but it does its job
response to : if using toObject<T> and naming a Type could you elaborate on this with an example please
There seem to be two ways to use this library. The first one uses the ToObject() method and will create a hierachy of JsonData objects that represent the original json using untyped Dictionaries Lists and arrays. The second approach allows you to use the generic ToObject<T> method. To use this you would do something like the following: public class MyDataObject { public string name; public int number; } with that class you could then do the following: MyData myData = new MyData(); string json = JsonMapper.ToJson(myData); MyData anotherMyData = JsonMapper.ToObject<MyData>(json); This would serialize the object to a json string and then deserialize it to a new object. This works with any kind of data, so if you get some data as a respnse from a server you would just create a class containing the respective fields and then you can use the ToObject<T> method to create an object of that type from a json string.
thank you for the reply.. unfortunately I'm still stuck Do i still need to implement the francksitbon code if I only want to write the file to an array and just display the text. (json contains all text). public string[] jsonTeamsSelected = new string[]; public string[] jsonDefSelected = new string[]; void ExtractTeams(string json) { JsonData jsonTeams = JsonMapper.ToObject(json); Debug.Log(jsonTeams["teams"].Count); jsonSetcount = jsonTeams["teams"].Count; for (int j = 0; j < jsonSetcount; j++){ jsonTeamsSelected[j] = jsonTerms["teams"][j]["team"].ToString(); jsonDefSelected[j] = jsonTerms["teams"][j]["defend"].ToString(); }
I am also trying to parse json on iOS, but cant find it working Code (csharp): JsonData jd = JsonMapper.ToObject(www.data.ToString()); net_amt=jd["totals"][0]["net_amt"].ToString(); net_gr=jd["totals"][0]["net_gr"].ToString(); net_amt_gr=jd["totals"][0]["net_amt_gr"].ToString(); net_vatav=jd["totals"][0]["net_vatav"].ToString(); net_total=jd["totals"][0]["net_total"].ToString();
You are trying to use the non-generic version of ToObject which also doesn't work for me. If you use the generic one it works.
Hello, could you elaborate on this? I'm having a similar problem. Not the exception, but the result of non-generic ToObject() is corrupt Thank you!
I think what he meant was calling ToObject<T>() instead of just ToObject. What is it you're trying to serialize / deserialize?
Oh, I misunderstood. But I debugged the problem and now LitJson works fine on my iOS, with non-generic ToObject(). The problem was in line 532 of JsonMapper.cs. Code (csharp): ((IDictionary) instance)[property] = ReadValue ( factory, reader); This makes ReadValue() called again and again before assignment and consume all the members of currently parsing object. I suppose this is an AOT compiler bug or something, but don't know exactly. Anyway separating call to ReadValue() and assignment and prevent optimizer from merging them again fixed the problem. In my case the problem was not an exception, but corrupt resulting data, so I don't know if this workaround will save everyone having similar issues.
As a UnityScript user I couldn't figure out, what to change in JsonMapper.cs. Could you include the code snippet you used to fix this, Takafumi? Thank you a lot StephanK! Your solution saved me. According to StephanK LitJson should be used like this in UnityScript: JSONData = LitJson.JsonMapper.ToObject.<LitJson.JsonData>(JSONText); instead of JSONData = LitJson.JsonMapper.ToObject(JSONText);