Click here to Skip to main content
12,949,601 members (69,737 online)
Rate this:
Please Sign up or sign in to vote.
Hi, can somebody help with the proper way to deserialize json data of this format:

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

The code i'am using is as 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<auto>>(json);
foreach (var item in persons)
    Console.Write("name:" + + " and age: " + item.age);

the class I'am mapping to is as below:

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

I keep getting nulls and empty strings for the name and age properties when I run this code. I will appreciate it a lot if someone can help me out.
Posted 25-Jul-12 22:20pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,489
CHill60 3,380
Maciej Los 2,913
Jochen Arndt 1,935
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 26 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100