-1

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());
juanferrer
  • 1,192
  • 1
  • 16
  • 29
FeelRightz
  • 2,777
  • 2
  • 38
  • 73
  • By the way, this isn't even Json array. Paste your code [here](http://json2csharp.com/) and you will get the right json data. `RootObject viewObj = JsonUtility.ToJson (www.text);` – Programmer Jun 29 '17 at 15:58
  • @Programmer I need to use which example you answer in the previous question ? seen I can't find any example for `List<>` – FeelRightz Jun 30 '17 at 01:43
  • The json is not even a list or array. Did you read my first comment? Please read that. – Programmer Jun 30 '17 at 03:52
  • I try `RootObject viewObj = JsonUtility.ToJson (www.text);` , and get this error `Cannot implicitly convert type `string' to `RootObject'` – FeelRightz Jun 30 '17 at 07:46
  • It wouldn't hurt to read the duplicated question. It shows how to do that. By the way way, my first comment is a typo. That should have been `RootObject viewObj = JsonUtility.FromJson(www.text)`. Now, if you have more problems read from where is says **4.TROUBLESHOOTING JsonUtility:** – Programmer Jun 30 '17 at 07:52

2 Answers2

0

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

AlameerAshraf
  • 872
  • 1
  • 13
  • 23
-1

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.

Suraj S
  • 1,019
  • 7
  • 18
  • have to `using SimpleJSON;` > – FeelRightz Jun 29 '17 at 12:45
  • Yeah! Sorry i missed that ! – Suraj S Jun 29 '17 at 12:47
  • Sorry, you **don't** have to use external library for json array. – Programmer Jun 29 '17 at 15:53
  • I'm sorry but as mentioned in the duplicate post, it has said that unity does not have an inbuilt class to deserialize a Jason array but requires you to write your own class already mentioned there, my interpretation was correct and for simplicity I suggested to use a separate library. Can you please explain the down vote @Programmer – Suraj S Jun 29 '17 at 16:01
  • 1.Your answer focused on Json array but the json in the question is not even Json array. Paste [here](http://json2csharp.com/) to verify. 2.You don't need to use external library like `SimpleJSON`. You can just make a simple function to support json array on top of `JsonUtility`. 3.Answering a duplicate. – Programmer Jun 29 '17 at 16:08
  • if I want to access to `watchlist` just `jsonObject.watchlist` ? – FeelRightz Jun 30 '17 at 01:44