Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 JSON MVC3
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:" + item.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
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Prasad_Kulkarni at 26-Jul-12 4:55am
   
Good references +5!
aspnet_regiis at 26-Jul-12 5:45am
   
Thank you Prasad .. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,725
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,589
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 26 Jul 2012
Copyright © CodeProject, 1999-2014
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