Unity : Assign json to List

808 Views Asked by At

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());
2

There are 2 best solutions below

6
On

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.

2
On

It means you want to deserialize the GET's Result object to its own type So, you need to use Newtonsoft Library to help you to deserialize the object. To get the library in the Package Manger Console Write the following Command

Install-Package Newtonsoft.Json

Or esily Download it from NuGet Package Manger. Then use the following Code to Deserialize The object that you have already

string TargetObject = YourObject; 
List<WatchList> Wl = JsonConvert.DeserializeObject<List<WatchList>>(TargetObject);

Try this to add it to Unity :

Make a folder: Assets/Plugins where you put the *.dll file and add it as a reference.

To add a reference you highlight the Analyzers in the Solution Explorer in Visual Studio and under Project > Add Reference you can find your *.dll file located in Assets/Plugin by browsing to it.

Or Try This Command

Install-Package Unity.Newtonsoft.Json -Version 7.0.0

Unity.Newtonsoft | NuGut