1

I`m using JsonHelper (https://stackoverflow.com/a/36244111) and set all in my program

UserInfo.cs

[Serializable]
public class UserInfo : MonoBehaviour {
    public string number;
    public string name;
}

And when I call UserInfo[] list = JsonHelper.FromJson<UserInfo>(www.downloadHandler.text);, I see

ArgumentException: JSON must represent an object type.

Please help me. Json [{"number":0,"name":"Bulbasaur"},{"number":1,"name":"Ivysaur"},{"number":2,"name":"Venusaur"},{"number":3,"name":"Charmander"},{"number":4,"name":"Charmeleon"},{"number":5,"name":"Charizard"},{"number":6,"name":"Squirtle"}]

mskalash
  • 25
  • 6
  • I'd guess this is the answer: https://stackoverflow.com/a/43233724. If not, can you please show us the JSON you're trying to deserialize? (i.e. please edit an example into the question) – Rup Aug 23 '18 at 11:03
  • I`m edit. Add json – mskalash Aug 23 '18 at 12:46
  • See the example class from the dup. It doesn't inherit from MonoBehaviour. You can't do that. – Programmer Aug 23 '18 at 14:31

1 Answers1

1

If you are using the wrapper class below...

using UnityEngine;
using System;

public static class JsonHelper
{
    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
        return wrapper.Items;
    }

    public static string ToJson<T>(T[] array, bool prettyPrint = false)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return JsonUtility.ToJson(wrapper, prettyPrint);
    }

    [Serializable]
    private class Wrapper<T>
    {
        public T[] Items;
    }
}

Then use it like this:

string JSONToParse = "{\"Items\":" + www.downloadHandler.text + "}";
UserInfo[] list = JsonHelper.FromJson<UserInfo>(JSONToParse);

Why?

"{\"Items\":" 

must match the name of the Array variable

public T[] Items;

EDIT:

Found your problem. Remove "MonoBehaviour" as inheritance from UserInfo. That should fix it. The serializer cannot serialize classes that inherit from monobehaviour, component, scriptableobject etc. Only plain classes (models) work.

[Serializable]
public class UserInfo 
{
    public string number;
    public string name;
}
Immorality
  • 2,164
  • 2
  • 12
  • 24
  • I am try but not work i have json: `[{"number":0,"name":"Bulbasaur"},{"number":1,"name":"Ivysaur"},{"number":2,"name":"Venusaur"},{"number":3,"name":"Charmander"},{"number":4,"name":"Charmeleon"},{"number":5,"name":"Charizard"},{"number":6,"name":"Squirtle"}]` and try return first but unity say null (( – mskalash Aug 23 '18 at 12:35
  • Can you post the rest of your code containing the call to the JsonHelper? – Immorality Aug 23 '18 at 12:49
  • Have you verified that you get a reponse by calling Debug.Log(www.downloadHandler.text) ? – Immorality Aug 23 '18 at 13:04
  • Yes I am check. He load `[{"number":0,"name":"Bulbasaur"},{"number":1,"name":"Ivysaur"},{"number":2,"name":"Venusaur"},{"number":3,"name":"Charmander"},{"number":4,"name":"Charmeleon"},{"number":5,"name":"Charizard"},{"number":6,"name":"Squirtle"}]` – mskalash Aug 23 '18 at 13:06
  • See my latest edit for the solution – Immorality Aug 23 '18 at 13:11
  • Thenks Its work – mskalash Aug 23 '18 at 13:13