Hi guy,
Would I have to create a new type of JSONparser? Or is there another (simple) way of adding values and replacing existing values to the JSON data array?
I have the following code:
function Start ()
{
var data;
data = JSONParse.JSONParse('{"request":{"foo":"0"}}');
Debug.Log(data['request']['foo']); ~#1
data['request']['foo'] = 1;
Debug.Log(data['request']['foo']); ~#2
data = JSONParse.JSONParse('{"request":{"foo":"2"}}');
Debug.Log(data['request']['foo']); ~#3
data = JSONParse.JSONParse('{"new_request":{"foo":"0"}}');
Debug.Log(data['new_request']['foo']); ~#4
Debug.Log(data['request']['foo']); ~#5
}
Debug.Log() results:
~#1: 0
~#2: 1
~#3: 2
~#4: 0
~#5: NullReferenceException: Object reference not set to an instance of an object
It appears the data var has been overwritten instead of adding the new_request. Which is logical of course but I want it to add. Like ‘+=’ can add a string to an existing string.
The reason I need this type of adding to work is because during my game I request data from my server with a www request. I want to be able to add the result of the request to a data array that has to be available during the whole game. But if the value already exists I’d like it to replace the previous value.
The JSONparser http://wiki.unity3d.com/index.php?title=JSONParse