- Home /
JSON must represent an object type
Hello,
When I try to use "JsonUtility.FromJson", I get "JSON must represent an object type", I looked at this answer, but I still don't get it.
public class JSONRead : MonoBehaviour {
public string url;
[Serializable]
public class RootObject
{
public int userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
}
IEnumerator Start()
{
RootObject myObject = new RootObject();
using (WWW www = new WWW(url))
{
yield return www;
Debug.Log(www.text);
if (www.isDone)
{
myObject = JsonUtility.FromJson<RootObject>(www.text);
Debug.Log(myObject.userId);
}
}
}
}
Well that JSON data is an array, not an instance of that object. Have you tried telling it to expect an array?
RootObject[] myObjects = JsonUtility.FromJson<RootObject[]>(www.text);
Actually, it looks like arrays weren't supported at least at some point during 2016 and I'm not sure if they're supported right now as I'm away from Unity.
Alternatively as a ugly option, you can try manually trim$$anonymous$$g the leading [
and trailing ]
in your www.text result, splitting that result by ,
, and parsing each substring as your object type.
I tried this:
RootObject[] myObject = JsonUtility.FromJson<RootObject[]>(www.text);
Debug.Log(myObject[1].userId);
And it says the same thing.
EDIT: As @Bunny83 points out, I am wrong and Arrays aren't yet supported!
Now if only I knew how to add strike-through formatting...
xxxxxxxxxxxxxxxxxxxxx but properties aren't, so you'll have to get rid of the { get; set; }
parts to make them fields.
[Serializable]
public class RootObject
{
public int userId;
public int id;
public string title;
public string body;
}
What version did add array support as root object? Are you sure about that?
well I'm less sure now! I checked the docs again and sure enough they say arrays aren't supported and have to be wrapped by a class. I think I'd just mis-remembered some dialogue tree stuff I'd been doing the other day with it, and it just happened that I chose to wrap my array in a class anyway :)
Answer by Bunny83 · May 07, 2018 at 02:59 PM
There are several issues here. First of all as other have already said Unity's JsonUtility does only support objects as top level nodes. So if your json consists of an array of objects Unity can't parse that. Furthermore the actual C# objects involved need to be serializable by Unity. So they have the same rules as the usual serialization system. That means only public fields or fields which have the SerializeField attribute are serialized. Properties won't work.
To solve both issues you can do this:
[Serializable]
public class UserObject
{
public int userId;
public int id;
public string title;
public string body;
}
[Serializable]
public class RootObject
{
public UserObject[] users;
}
myObject = JsonUtility.FromJson<RootObject>("{\"users\":" + www.text + "}");
This of course assumes that the JSON actually represents an array. So your downloaded json should looks something like this:
[
{
"userId": 5,
"id": 42,
"title":"Douglas Adams",
"body":"foobar"
},
{
"userId": 6,
"id": 43,
"title":"Ford",
"body":"---"
}
]
If your text does not looks like this but just
{
"userId": 5,
"id": 42,
"title":"Douglas Adams",
"body":"foobar"
}
You should just use
UserObject myObject = JsonUtility.FromJson<UserObject>(www.text);
If this doesn't solve your problem you should include an example of your json data in your question.
I know it's over a year later but THAN$$anonymous$$ YOU to Bunny83 for the clear explanation and example. I've been struggling to do something similar to OP and until I saw this thread, was getting nowhere. Thanks!
Thanks, I'm setting up a simple scoreboard and this was very helpful.
Answer by thorur · Mar 16, 2019 at 04:11 PM
Actually im kinda noob at this but i would suggest this option:
download package from https://www.nuget.org/packages/Newtonsoft.Json/
change package extension to zip
go to Bin>netstandard2.0
get dll file
copy dll file to your assets And use this serializer insted.
Answer by santhosh98kumar · Sep 30, 2019 at 07:51 AM
I followed this same, but still, I am getting the same error "JSON must represent an object type" here is my code. [Serializable] public class Things { public ThingGearKeys thingGearKeys; public string thingplacement; public string lastupduser; public long lastupdtime; public string isdeleted; public object schema; } [Serializable] public class RootObject { public Things[] listOfThings; } String json = www.downloadHandler.text.ToString(); RootObject rootObject1; rootObject1 = JsonUtility.FromJson(json);
//this is my JSON response
[{\"thingGearKeys\":{\"gearid\":\"G000002\",\"customerid\":\"C000001\",\"smartplaceid\":\"DLG6VI4TH1A\",\"mccid\":\"M000001\",\"thingId\":\"00000002\"},\"thingplacement\":\"top\",\"lastupduser\":\"customeruser14\",\"lastupdtime\":1562917569083,\"isdeleted\":\"N\",\"schema\":null}]
You did not really follow what I've written. Look carefully at this line:
myObject = JsonUtility.FromJson<RootObject>("{\"users\":" + www.text + "}");
I wrap the json text in an object (with the name "users"). Your json text still has an array as root object.
Next time please ask a seperate question and do not post an answer to other people's questions which does not answer the question. Also please lookup the markdown help how to properly format a code block. You used an inline code block for your code and no formatting for your json text.
Thanks, just save me 60 $$anonymous$$ of headache.
Answer by Aweb93 · Nov 18, 2021 at 04:39 PM
I know this is pretty old, but I actually had to do this to make the suggested answer work.
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>("{\"Items\":" + json + "}");
return wrapper.Items;
}
Answer by TaewoongYoon · Jan 06 at 06:33 PM
,In my case, use Newtonsoft json parser instead of JsonUtlity and deserialize as an array. Works well
void jsonToList(string json)
{
var list = Newtonsoft.Json.JsonConvert.DeserializeObject<rankingData[]>(json);
}