-1

Json result returns Array of objects some times and a single object if single record exists.so i want to Parse the json result to my model class which is a list,so if json returns an array then its working fine..if it is a single object i m getting this error..

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Chefsportalen.Models.JsonObjectModels.ListDevicesByLabelModel+Device]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'devices.device.@id', line 1, position 90.

public class listAllAppsModelcs
{
    public string deviceUuid { get; set; }
    public List<App> clientApps { get; set; }
    public class App
    {
        public string @id { get; set; }
        public string appName { get; set; }
        public string assetName { get; set; }
        public string bundle { get; set; }
        public int inventoryAppId { get; set; }
        public string platform { get; set; }
        public object reportedAppName { get; set; }
        public string type { get; set; }
        public object version { get; set; }
    }
    public class Apps
    {
        public List<App> clientApp { get; set; }

    }
}

this is my model class..

and my json responce is this

{
    "deviceUuid": "dd77202b-ec50-4c0b-95d3-8c742d171d31",
    "clientApps": {
        "clientApp": {
            "@id": "203131",
            "appName": "MobileIron 6.2",
            "assetName": "MobileIron 6.2",
            "bundle": "com.mobileiron.phoneatwork",
            "inventoryAppId": 6085,
            "platform": "iOS 8.1",
            "reportedAppName": "MobileIron",
            "type": "APPLICATION",
            "version": 6.2
        }
    }
}

Please Help me in fixing this issue..any help will be appriciated..thanks..

Community
  • 1
  • 1
Sagar Jagadesh
  • 237
  • 5
  • 14
  • 1
    Show some source code, would really help in diagnosing your problem. Specifically, show the javascript and methods that are involved in this process that is showing the error – Pseudonym Apr 03 '15 at 12:39
  • 1
    possible duplicate of [How to handle both a single item and an array for the same property using JSON.net](http://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n) – har07 Apr 03 '15 at 13:03

2 Answers2

0

Check your error. When you get a single item and try to deserialize it into a list, the error starts with Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

You have an unreliable JSON object. Maybe it'll be a list, maybe not. But you don't know that at runtime. That means you're going to need to do it dynamically. Json.NET can do this for you by deserialializing it into a dynamic object.

public static void Run()
{
    string[] jsons = { "[1, 2, 3]", "1" };


    foreach (var json in jsons)
    {

        List<int> myInts;

        dynamic d = JsonConvert.DeserializeObject(json);
        if (d is IEnumerable)
        {
            myInts = JsonConvert.DeserializeObject<List<int>>(json);
        }
        else
        {
            myInts = new List<int> {JsonConvert.DeserializeObject<int>(json)};
        }
        foreach (var myInt in myInts)
        {
            Console.Write(myInt);
        }
        Console.WriteLine();
    }
}

The above will print 123 1

We've now handled a situation where the JSON could be a list of ints or a single int by deserializing dynamically first and checking if it was a list.

Pharylon
  • 9,796
  • 3
  • 35
  • 59
0

There is a really nice tool http://json2csharp.com/ that converts JSON objects to C# classes.

Your JSON object should look like this in C#

public class ClientApp
{
    public string __invalid_name__@id { get; set; }
    public string appName { get; set; }
    public string assetName { get; set; }
    public string bundle { get; set; }
    public int inventoryAppId { get; set; }
    public string platform { get; set; }
    public string reportedAppName { get; set; }
    public string type { get; set; }
    public double version { get; set; }
}

public class ClientApps
{
    public ClientApp clientApp { get; set; }
}

public class RootObject
{
    public string deviceUuid { get; set; }
    public ClientApps clientApps { get; set; }
}
Arlind Hajredinaj
  • 8,380
  • 3
  • 30
  • 45