Newsoft Json deserialize into products Collection not working Newsoft Json deserialize into products Collection not working json json

Newsoft Json deserialize into products Collection not working


You can deserialize it this way (but you'll have somehow to reformat your JSON string):

from

var PDS = "{"products":[{"_id":"58b2","code":"59034","name":"somename1","imgAddress":"https://someimageurl/.../.jpg","queryCount":0},{"_id":"58b3","code":"59035","name":"somename2","imgAddress":"https://someimageurl2/.../.jpg","queryCount":1}]}";//PDS is actually very long string and the array products has a lot of objects but I am only writing 2 products here for simplicity.

to

var PDS = "[{'_id':'58b2','code':'59034','name':'somename1','imgAddress':'https://someimageurl/.../.jpg','queryCount':0},{'_id':'58b3','code':'59035','name':'somename2','imgAddress':'https://someimageurl2/.../.jpg','queryCount':1}]";//PDS is actually very long string and the array products has a lot of objects but I am only writing 2 products here for simplicity.

Then

var pds = JsonConvert.DeserializeObject<List<Product>>(PDS);

More infos --> Deserialize a collection

No need of creating another class as suggested!

EDIT:Actually you get this exception because your JSON string represents an object 'products' containing a list of 'Product' and this doesn't directly relate to your root class.

This is what your current JSON look like:

Your current JSON string

Your JSON string result should be something like that instead:

var PDS = "[{'_id':'58b2','code':'59034','name':'somename1','imgAddress':'https://someimageurl/.../.jpg','queryCount':0},{'_id':'58b3','code':'59035','name':'somename2','imgAddress':'https://someimageurl2/.../.jpg','queryCount':1}]";

Represented as following (notice the slight difference):

Correct JSON string for List<Product> type

Using this JSON string, it will work with no exception raised:

var pds = JsonConvert.DeserializeObject<List<Product>>(PDS);

Or, if you can't change the resulting JSON string in PDS, you can do what J. Tuc suggested (via a new "RootClass").


your JSON string had to be changed to get the code to compile:

var PDS = "{'products':[{'_id':'58b2','code':'59034','name':'somename1','imgAddress':'https://someimageurl/.../.jpg','queryCount':0},{'_id':'58b3','code':'59035','name':'somename2','imgAddress':'https://someimageurl2/.../.jpg','queryCount':1}]}";

You need another object with a collection of Product as a property called products in order to deserialize your JSON

This may not be the best solution but it works:

Create another class:

public class RootObject{    public List<Product> products;}

use this code to deserialize your JSON:

var pds = JsonConvert.DeserializeObject(PDS, typeof(RootObject));

if you are in control of your JSON string you might consider changing it to this (removing 'products' property and ending up with just an array of products in the JSON string):

var PDS = "[{'_id':'58b2','code':'59034','name':'somename1','imgAddress':'https://someimageurl/.../.jpg','queryCount':0},{'_id':'58b3','code':'59035','name':'somename2','imgAddress':'https://someimageurl2/.../.jpg','queryCount':1}]";

Then you might be able to use the solution proposed by TaiT's (deserializing directly to a List of Product) :

var pds = JsonConvert.DeserializeObject<List<Product>>(PDS);