简体   繁体   中英

Can't deserialize JSON into C# POCO

I can't convert JSON Object into C# class object I've tried many things but the same error pops up:

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[CoderwallDotNet.Api.Models.Account]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.

I am using this JSON and tring to get the response in windows 8.1 application.

[
{
    "id": 1,
    "time": 40,
    "srcLong": 35.909124,
    "srcLat": 31.973628,
    "destLong": 35.898258,
    "destLat": 31.985622,
    "subSites": [
        {
            "id": 1,
            "name": "location1"
        },
        {
            "id": 2,
            "name": "location2"
        },
        {
            "id": 3,
            "name": "locaion3"
        }
    ]
}]

and the I tried to read from the JSON using webclient but its not working it can't recognize it so I am using Newtonsoft.Json and I creted these class to get the response.

public class SubSite
{
    public int id { get; set; }
    public string name { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public int time { get; set; }
    public double srcLong { get; set; }
    public double srcLat { get; set; }
    public double destLong { get; set; }
    public double destLat { get; set; }
    public List<SubSite> subSites { get; set; }
}

var serviceUri = "http://localhost:24728/api/sites";     

var client = new HttpClient();            

var response = await client.GetAsync(serviceUri);            

var datafile = await response.Content.ReadAsStringAsyn();                        

List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(datafile); 

test1234.Text = data.ToString();//i am trying to for eample destlat

but I can't get the value I am getting the response and everything is fine but idk how to put it into object and use it where ever I want. For example, I want to get the value of time and the other but I have problem with List of subsites or get the location inside the subsites or srclong or srclat and here is the project how to get the Json to c# object: https://onedrive.live.com/?cid=e648f5a0f179f346&id=E648F5A0F179F346%218429&ithint=folder,rar&authkey=!ALKlJdwsb8ER2FA

在此处输入图片说明 This works fine:

var data = JsonConvert.DeserializeObject<RootObject>(datafile) 

You can deserialize into a dynamic object if you are not too particular about losing intellisense. One advantage to using a dynamic is that you don't have to create classes just to be able to deserialize and also that you don't need to update your class structures if the returned data has changed. As an example you can do this:

dynamic jsonValue = JsonConvert.DeserializeObject(jsonData);
foreach (dynamic rootObject in jsonValue)
{
   theData.destLong.Value <-- use it anyway you want, store in a variable, etc.
   // to get to each of the subSite in the rootObject
   foreach (dynamic subSite in rootObject.subSites)
   {
       subSite.id.Value <-- returns an int based on your data
       subSite.name.Value <-- returns a string based on your data
   }
}
// where jsonData constains the json string you posted

The foreaeach is important because the data you posted will result into an array. You can also just do a jsonValue[0] but watch out for errors for null values, which you should be checking anyway starting with the returned json string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM