Tag Archives: Unity – json array

Unity – JSON array deserialization

Editor’s note

When editors want to load unity

    private string getjson()
    {
        String result;

        Uri uri = new Uri(string.Format("http://localhost:8080/rest/scene"));

        HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            result = reader.ReadToEnd();
        }

        Debug.Log("[json]:" + result);

        return result;
    }

    private SceneEntity[] JsonToEntity(string json)
    {
        json = "{\"Items\":" + json + "}";
        Debug.Log("[json]:" + json);
		 
        SceneEntity[] scenes = JsonHelper.FromJson<SceneEntity>(json);

        foreach (SceneEntity entity in scenes)
        {
			Debug.Log("[scene][id]:" + entity.id + " [name]:" + entity.name);
        }

		Debug.Log ("[0][id]:" + scenes[0].id);
		Debug.Log ("[1][id]:" + scenes[1].id);

        return scenes;
    }