Hi, I have donwloaded Unity for evaluation and am trying to build POC to identify any issues.
I have hit my first problem, I am calling a Web URL that returns a response in JSON format. Normally I would expect to use browser JavaScript code similar to the following to process the response:
var JSONFile = "someVar = { 'color' : 'blue' }"; // example of what is received from the server.
eval(JSONFile); // Execute the javascript code contained in JSONFile.
print(someVar.color); // Outputs 'blue'
When I try this in Unity the print statement fails because ‘color’ is not an attribute of someVar.
I have read in the forums that eval is supported in Unity 2 but should not be used?
I tried the following test in Unity to see if a dynamic object would work locally:
var myFirstJSON = { "firstName" : "John",
"lastName" : "Doe",
"age" : 23 };
document.writeln(myFirstJSON.firstName); // Outputs John
document.writeln(myFirstJSON.lastName); // Outputs Doe
document.writeln(myFirstJSON.age); // Outputs 23
When I run this in Unity I get the following error:
error BCE0019: ‘firstName’ is not a member of ‘Boo.Lang.Hash’.
I have seen one other post covering JSON support, the recommendation there was to use C# and .Net to do low level processing.
So I guess my first questions is, should I be able to use eval to parse a JSON payload and return me an object structure that I can work with in Unity JavaScript?
If not then my second question is what is the recommended way to pass data from Web calls into Unity scripts and parse them, is it XML?
On the Web Site it states:
Are there any examples/tutorials covering this aspect of scripting?
Any thoughts appreciated.
Thanks in advance