- Home /
Why are JSON Libraries even necessary? Isn't it Native?
I haven't seen this question really addressed here properly. I realize that UnityScript IS NOT proper JavaScript. That being said, since UnityScript is built on JavScript, and since JavaScript can handle JSON objects natively (using basic dot synax like var firstName=names[0].name), can't UnityScript handle JSON objects in the same way?
For example, for this JSON Object:
"names"[ {"name":"Bob"}, {"name":"Steve"}, {"name":"Joe"} ]
(The formatting there may not be exactly right, I just made it up for an example.)
So in UnityScript, would it not work if I did: var:String bobsname= jsonObject.names[0].name;
where bobsname now holds the string "Bob" ? Please let me know if this is true. It would be a real bummer for UnityScript to not natively handle JSON objects.
Answer by hiddenspring81 · Jun 01, 2013 at 07:42 PM
Let me preface this by saying that I exclusively write my Unity code in C#, and that my only JavaScript experience has been in web development.
Suppose that you receive a JSON formatted response string from a web service request. You still need a library to convert the response string into an instance of an object. In the example you provided, you had a string
{"names": [{"name":"Bob"}, {"name":"Steve"}, {"name":"Joe"}]}
But before you can do anything useful with it, you first need to convert it into an instance of a object. Once it's been converted into an object, you should then be able to query that object, using "dot" notation.
So say i use $$anonymous$$iniJSON to convert it to an instance of an object (thats object with a lowercase "o") called result. Then i can just do result.names[0].name to get to the string "Bob"?
I really like $$anonymous$$iniJSON. I've never used it in UnityScript, so I'm not sure exactly how you'd use it. Give it a shot and let me know what happens!
Your answer
Follow this Question
Related Questions
Accessing JSON in unityscript 0 Answers
Javascript: Disable warnings 1 Answer