1

Can somebody help me with the proper way to deserialize json in this format:

[ {person: {name: "James", age:26}}, {person: {name: "Mary", age:36}}, {person: {name: "Kofi", age:46}} ] 

The code I'm using is below:

WebRequest request = WebRequest.Create("url"); WebResponse response = request.GetResponse(); string json; using (var sr = new StreamReader(response.GetResponseStream())) { json = sr.ReadToEnd(); } var serializer = new JavaScriptSerializer(); var persons= serializer.Deserialize<List<response>>(json); foreach (var item in persons) { Console.Write("name:" + item.name + " and age: " + item.age); } 

The class I'm mapping to is below:

public class person { public string name{get;set;} public int age{get; set;} } public class response { public person person {get;set;} } 

I keep getting nulls and empty strings for the name and age properties when I run this code. I would appreciate it a lot if someone could help me out.

5
  • 3
    You are mapping to the person class, but in your deserializer you use the "auto" class? Commented Jul 26, 2012 at 8:29
  • 2
    are you sure this is valid JSON? I think you need more " around things like "person" Commented Jul 26, 2012 at 9:16
  • jsonlint.com seems to agree with me Commented Jul 26, 2012 at 9:17
  • I had to make call item.person.name and item.person.age in the foreach loop Commented Jul 26, 2012 at 10:51
  • possible duplicate of Json Deserialize C# Commented Jul 26, 2012 at 19:58

5 Answers 5

5

It's also possible with the use of the build-in DataContractJsonSerializer class. See example:

WebRequest request = WebRequest.Create("url"); WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { Type serializationTargetType = typeof(List<person>); DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(serializationTargetType); List<person> jsonDeserialized = (List<person>)jsonSerializer.ReadObject(stream); } 
Sign up to request clarification or add additional context in comments.

1 Comment

This won't work. The JSON structure shown by the OP doesn't represent an array of person. So using person[] as type will crash the serializer.
3

Solution

My solution is very easy but you must have installed:

nuget json

Example

using Newtonsoft.Json; var url = "http://your-url"; var json = JsonConvert.DeserializeObject<Person[]>(new WebClient().DownloadString(url)); List<Person> users = new List<Person>(); foreach (var user in json) { users.Add(new Person() { name = user.name, age = user.age }); } 

Comments

2

You could define models that will reflect your JSON structure:

public class Person { public string Name { get; set; } public int Age { get; set; } } public class Row { public Person Person { get; set; } } 

and then deserialize to an array of rows:

var serializer = new JavaScriptSerializer(); var rows = serializer.Deserialize<Row[]>(json); foreach (var row in rows) { Console.Write("name: {0} and age: {1}", row.Person.Name, row.Person.Age); } 

Comments

1

I had to make call item.person.name and item.person.age in the foreach loop and make sure all properties in the person class were of type string.

Comments

0

If what's you need is to deserialize a response from a web server I would use some framework that does all of this together.

I've used http://restsharp.org/ and it works great.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.