- Home /
Improper JSON formatting when decode JSON in Unity3D
Hi people,
I am using the JSON parser from the Unify Community Wiki as below:
http://www.unifycommunity.com/wiki/index.php?title=JSONObject
I follow the instruction in setting up the JSON parser and this is how i call the JSON parser as below:
However, i keep getting the error as below:
improper JSON formatting:file://D:/Project Files/Unity Project (Jukebox_r10)/Assets/Resources/sample.json UnityEngine.Debug:LogWarning(Object) JSONObject:.ctor(String) (at Assets/Customized Script/JSONObject.cs:126) Startup:Update() (at Assets/Customized Script/Startup.cs:20)
Can any kind soul enlight me on what is really going wrong in my script?
Thanks in advance. :)
This is kind of a specialist problem! I use a certain amount of serialization in my work, but that's all either straight binary, or X$$anonymous$$L- I'm not really sure I can help you here!
Thank you for your reply. Are you indicating in order to extract JSON, i will need to come out with my own JSON parser?
As I said, I really don't know- I have virtually no knowledge of this specific system. You might have one already, I don't know.
just a guess, but is it really getting the file sample.json from your file system or trying to parse the string pertaining to the location? I assume you've checked the file itself and it parses ok outside unity. Try hard-coding the json string within your script and decoding that just to check it works.
Answer by gfr · Oct 23, 2011 at 01:56 PM
As Jace guesses in the comments, JSONObject
doesn't take an encoded string but a filename. With what you have it tries to decode the filename as JSON, which of course fails.
So you have to load the JSON string yourself:
string encodedString = LoadStringFromFile(); // your function...
JSONObject j = new JSONObject(encodedString);
Hi Jace & gfr,
Thank you for the reply. Can you give me an idea how the LoadStringFromFile() work?
That's for you to implement, e.g.:
StreamReader sr = new StreamReader(path);
string encodedString = sr.ReadToEnd();
sr.Close();
Hi gfr,
Will give it a try let you guys know if everything went through.
Thanks again for the assistance again. :)
I realized that the JSONObject only work on certain JSON if you got the format that is been desired by the JSONObject Class. So ins$$anonymous$$d, i used the LitJSON and everything work pretty fine.
Your answer
Follow this Question
Related Questions
C# JSON library 6 Answers
IOS Parse Json 0 Answers
Is there a reason not to use system.json.dll? 2 Answers
SimpleJSON array problem 0 Answers
How to import Long and Lat (GPS Info) from a flat file and render the points. 1 Answer