0

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 (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CoderwallDotNet.Api.Models.Account]' because the type requires a JSON array (e.g. [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

5
  • 3
    Just as a side note: it's spelled JSON (JavaScript Object Notation), but it is pronounced like "Jason" :) Commented Jul 7, 2015 at 7:13
  • 1
    Given the error has: System.Collections.Generic.List``1[CoderwallDotNet.Api.Models.Account] I think you are not showing the correct code, or the correct error. Commented Jul 7, 2015 at 7:46
  • I've edited to correct your terms a little; the C# class is the POCO. The JSON is just JSON, it can't be POCO because POCO stands for Plain Old CLR Object. JSON knows nothing of the CLR. Commented Jul 7, 2015 at 7:50
  • There lies the interest of serialization. Commented Jul 7, 2015 at 9:06
  • System.Collections.Generic.List``1[CoderwallDotNet.Api.Models.Account] sry that was from other post I found on net it was the same error run the code and u will see it why else I will share the project ?? because I really need help that's the true project Commented Jul 7, 2015 at 21:30

2 Answers 2

1

enter image description hereThis works fine:

var data = JsonConvert.DeserializeObject<RootObject>(datafile) 
Sign up to request clarification or add additional context in comments.

3 Comments

I know but the problem I cant access the data Idk how to access it the values how to get the value separately.
You can access the properties from the data object
but I don't know how to access the properties from data object. show me example that's my problem I am stuck here.
0

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.

2 Comments

ok thank u I will try it and give you the result. and I will not need the data folder rootobject and subsite clases ??
Based on your posted sample, there is no "data" at root. If there is and you have something like {"data": [ ... ]} then just do a foreach(dynamic rootObject in jsonValue.data) plus the rest of the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.