Search code examples
c#arraysjsonserializationjson.net

Cannot deserialize the current JSON array (e.g. [1,2,3])


I am trying to read my JSON result.

Here is my RootObject

public class RootObject
{
public int id { get; set; }
public bool is_default { get; set; }
public string name { get; set; }
public int quantity { get; set; }
public string stock { get; set; }
public string unit_cost { get; set; }
}

Here is my JSON result

[{"id": 3636, "is_default": true, "name": "Unit", "quantity": 1, "stock": "100000.00", "unit_cost": "0"}, {"id": 4592, "is_default": false, "name": "Bundle", "quantity": 5, "stock": "100000.00", "unit_cost": "0"}]

Here is my code to read the result

     public static RootObject GetItems(string user, string key, Int32 tid, Int32 pid)
    {
        // Customize URL according to geo location parameters
        var url = string.Format(uniqueItemUrl, user, key, tid, pid);

        // Syncronious Consumption
        var syncClient = new WebClient();

        var content = syncClient.DownloadString(url);

        return JsonConvert.DeserializeObject<RootObject>(content);

    }

But I am having a problem with this error :

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'avaris_cash_salepoint.BL.UniqueItemDataRootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

at this line: return JsonConvert.DeserializeObject(content);

Any help to fix this error ?


Solution

  • I think the problem you're having is that your JSON is a list of objects when it comes in and it doesnt directly relate to your root class.

    var content would look something like this (i assume):

    [
      {
        "id": 3636,
        "is_default": true,
        "name": "Unit",
        "quantity": 1,
        "stock": "100000.00",
        "unit_cost": "0"
      },
      {
        "id": 4592,
        "is_default": false,
        "name": "Bundle",
        "quantity": 5,
        "stock": "100000.00",
        "unit_cost": "0"
      }
    ]
    

    Note: make use of http://jsonviewer.stack.hu/ to format your JSON.

    So if you try the following it should work:

      public static List<RootObject> GetItems(string user, string key, Int32 tid, Int32 pid)
        {
            // Customize URL according to geo location parameters
            var url = string.Format(uniqueItemUrl, user, key, tid, pid);
    
            // Syncronious Consumption
            var syncClient = new WebClient();
    
            var content = syncClient.DownloadString(url);
    
            return JsonConvert.DeserializeObject<List<RootObject>>(content);
    
        }
    

    You will need to then iterate if you don't wish to return a list of RootObject.


    I went ahead and tested this in a Console app, worked fine.

    enter image description here