3

I have problems with json deserialization , below is my json

{
 "_id" : ObjectId("56bc28c436b252c406a67f17"),
 "empname": "dhiraj",
 "empcode": "123a",
 "level": {
 "levelID": 3,
 "levelDescription": "manager",
 "levelCode": "mg"
 },
 "Address": [
 {
 "Home": {
 "streetname": "Home",
 "city": "bbb",
 "state": "aaa"
 }
 },
 {
 "Office": {
 "streetname": "ofc",
 "city": "ccc",
 "state": "ddd"
 }
 }
 ]
}

And for above json the object classes are like

public class Employee
{
 public ObjectId _id { get; private set; }
 public string empname { get; set; }
 public string empcode { get; set; }
 public List<Level> level { get; set; }
 public List<Address> Address { get; set; }
}
public class level
{
 public string levelID { get; set; }
 public string levelDescription { get; set; }
 public string levelCode { get; set; }
}
public class Address
{
 public List<Home> Home { get; set; }
 public List<office> Office { get; set; }
}
public class Home
{
 public string streetname { get; set; }
 public string city { get; set; }
 public string state { get; set; }
}
public class office
{
 public string streetname { get; set; }
 public string city { get; set; }
 public string state { get; set; }
}

i tried to deserialize it using below code

Employee empobj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Employee>>(jsonData);

but got an error as

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

How can i fix it? Is there any way, the json result is from mongodb c# query.

demonplus
5,83112 gold badges55 silver badges73 bronze badges
asked Feb 11, 2016 at 7:30
8
  • Well the JSON you've given isn't a List<Employee> - it's not an array at all. It's a single Employee. So use DeserializeObject<Employee>. Heck, I wouldn't expect the code you've given to even compile, given that you're assigning it to an Employee variable... Commented Feb 11, 2016 at 7:32
  • @jon : while posting it to here i made a mistake actually it is List<Employee> empobj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Employee>>(jsonData); this is what i am trying for and also i have just shared one emplyee record it will be multiple in my case you can consider an array of employee record . Commented Feb 11, 2016 at 8:50
  • But the JSON you've given is simply not an array. It's a single object. That's what the deserializer is complaining about. If you add an extra [ at the start and a ] at the end, I suspect it'll work fine... but there's a huge difference between "an array with one element" and "one element". Commented Feb 11, 2016 at 8:51
  • @jon:i tried adding [ at the start and end ] to json but still same error , i think there is something wrong with my class structure according to the json Commented Feb 11, 2016 at 9:01
  • Well, you've got the same problem with address, home and office - your JSON specifies an object, but your class declares a list. It's also not clear what the ObjectId part of your JSON is - that part is invalid JSON, basically. Commented Feb 11, 2016 at 9:05

1 Answer 1

3

There are several problems here:

  • The code you've given won't compile, as you've specified a class called level but used it as Level
  • You're trying to deserialize a List<Employee>, but your JSON only specifies a single Employee object; that's not the same as an array of objects containing a single object
  • Your JSON is invalid - ObjectId("56bc28c436b252c406a67f17") simply isn't a valid value in JSON. It may be that Json.NET has some support for this oddity, but it would be better if you could use valid JSON
  • Your Address class specifies a List<Home> for the Home property, and likewise for the Office property, but again the JSON just specifies an object value, not an array. Likewise for the level property.

Additionally, the fact that you've got separate classes for Home and Office is pretty nasty, as is the mixture of naming conventions. The JSON structure of the addresses is far from ideal, but I guess you can't fix that.

I can't really fix the ObjectId problem, but I'd structure the classes as:

public class Employee
{
 [JsonProperty("_id")]
 public ObjectId Id { get; private set; }
 [JsonProperty("empname")]
 public string Name { get; set; }
 [JsonProperty("empcode")]
 public string Code { get; set; }
 [JsonProperty("level")]
 public Level Level { get; set; }
 [JsonProperty("Address")]
 public List<Address> Addresses { get; set; }
}
public class Level
{
 [JsonProperty("levelID")]
 public string Id { get; set; }
 [JsonProperty("levelDescription")]
 public string Description { get; set; }
 [JsonProperty("levelCode")]
 public string Code { get; set; }
}
// This structure is unfortunate, but imposed by the JSON
public class Address
{
 [JsonProperty("Home")]
 public StreetAddress Home { get; set; }
 [JsonProperty("Office")]
 public StreetAddress Office { get; set; }
}
public class StreetAddress
{
 [JsonProperty("streetname")]
 public string StreetName { get; set; }
 [JsonProperty("city")]
 public string City { get; set; }
 [JsonProperty("state")]
 public string State { get; set; }
}

Aside from ObjectId, that will parse the JSON you've given using:

var employee = JsonConvert.DeserializeObject<Employee>(json);
answered Feb 11, 2016 at 9:30

3 Comments

is this StreetAddress in Address class needed i hope you missed it out while writing your answer.
@dhiraj: Sorry, yes - not sure what happened there. Have a look now.
:thanks the class structure worked for me as i am new to mongodb and c# , was trying to map mongodb document to c# classes and also no deserialization required as it being taken care by c# mongo driver and bson drivers. thanks once again

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.