Below JSON is WWW
GET result
{
"status":200,
"watchlist":[
{
"Category":"Movie",
"ShowList":[
{
"id":"59534a851339762f38e16ad7",
"Name":"Play zombie tonight",
"Description":"descblablabla",
"IMDB":1,
"Tag":[
"horror",
"comedy"
],
"Picture":{
"id":"59534a8b1339762f38e16b6d",
"Url":"imgurlbla",
"DisplayOrder":0
}
}
]
},
{
"Category":"Variety",
"ShowList":[
{
"id":"59532bf51339742f380d4cb1",
"Name":"Yearning for life",
"Description":"descblablabla",
"IMDB":1,
"Tag":[
"reality show"
],
"Picture":{
"id":"59532bfb1339742f380d4d3a",
"Url":"imgurlbla",
"DisplayOrder":0
}
}
]
}
]
}
I want to assign the result to List<>
, so can I easily access the value in WatchList
. Below is the code I tried.
List<WatchList> viewitemlist = JsonUtility.ToJson (www.text);
I also have tried a library called LitJSON :
string json = www.text;
JsonData jsonObject = JsonMapper.ToObject(json);
List<On360WatchList> viewitemlist =
JsonMapper.ToObject<List<On360WatchList>> (jsonObject ["watchlist"].ToJson());
I had a similar issue when parsing a JSON array in Unity, you cannot parse a JSON array with JsonUtility, you have to use an external library, SimpleJSON worked for me, just paste the .cs file in your scripts folder. You can download the JSON library from here SimpleJSON. Scroll down to the SimpleJSON.cs section, copy everything to a new .cs file in the scripts folder and parse the response using
using SimpleJSON; var jsonObject = JSON.Parse(response)
and you are good to go! You can access it as an array or assign it to a Watchlist.