- Home /
JsonUtility and Arrays [Error - "JSON must represent an object type"]
Hi all,
I know this has been asked previously and I have looked through those post. I am having a hard time wrapping my brain around what I have to do to get an array to work with the JsonUtitlity Serialization, or if it's even possible.
Heres what I have:
[System.Serializable]
public class JSONObject : System.Object {
public string[] values;
}
public void request (params string[] values){
JSONObject JSON = new JSONObject ();
JSON.values = values;
StartCoroutine(webCoro(JsonUtility.ToJson(JSON)));
}
void response (string JSONstring) {
JSONObject JSON = JsonUtility.FromJson <JSONObject> (JSONstring);
}
IEnumerator webCoro (string data){
string URL = "http://" + host + ":" + port;
WWW request = new WWW (URL, encoding.GetBytes (data));
while (!request.isDone) {
yield return null;
}
Debug.Log ("Request Error: " + request.error);
response (request.text);
}
public void test (){
request ("script", "action", "test");
}
When I run test () I get:
ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson[JSONObject] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:24) Web.response (System.String JSONstring) (at Assets/Web.cs:36) Web+c__Iterator0.MoveNext () (at Assets/Web.cs:50)
What do I need to do to make this happen?
Thanks in advance, Sean
Answer by Bunny83 · Feb 29, 2016 at 01:02 PM
Well, how does your JSON data looks like? The deserializer expects something like this:
{ "values":["one","two","three"]}
How does your data look like? Is it just an array? The JsonUtility seems to only work with objects as root element. If your data looks like this:
["one","two","three"]
You might want to wrap it manually in an object like this:
string yourJSONArray;
string JSONToParse = "{\"values\":" + yourJSONArray + "}";
So ultimately the answer was that my node.js code was using JSON.stringify (myRequest) on a request that was already in JSON format giving me :
"{\"values\":[\"script\",\"action\",\"test\"]}"
So when I cut that out I received a response more to the tune of:
{"values":["script","action","test"]}
And this worked out, thanks everyone for your responses, they helped me come to this conclusion.
Sean
Answer by STBarnard · Feb 29, 2016 at 01:11 PM
Had my node server echo some data back.
"{\"values\":[\"script\",\"action\",\"test\"]}"
That's what i'm working with.
I just tested your code without the WWW request, and it seems to serialise the array fine.
The string should look like this: {"values”:[“script”,”action”,”test”]}
Did you see this answer? http://answers.unity3d.com/questions/844423/wwwtext-not-reading-utf-8-text.html
I tried this based off that post:
IEnumerator webCoro (string data){
string URL = "http://" + host + ":" + port;
WWW request = new WWW (URL, encoding.GetBytes (data));
while (!request.isDone) {
yield return null;
}
Debug.Log ("Request Error: " + request.error);
string responseString = System.Text.Encoding.UTF8.GetString(request.bytes, 3, request.bytes.Length - 3);
Debug.Log (responseString);
response (responseString);
}
It just cut off the opening brackets:
"values\":[\"script\",\"action\",\"test\"]}"