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

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 + "}";

Had my node server echo some data back.

“{"values":["script","action","test"]}”

That’s what i’m working with.