JSON格式解析为Object

时间:2014-07-09 13:11:27

标签: .net json

我需要打算使用这个JSON:

[
      {
        "uid": "15410773191172635989",
        "first_name": "Евгений",
        "last_name": "Маслов",
        "nick": "maslov",
        "email": "emaslov@mail.ru", 
        "sex": 0, 
        "birthday": "15.02.1980",
        "has_pic": 1, 
        "pic": "http://avt.appsmail.ru/mail/emaslov/_avatar",
        "pic_small": "http://avt.appsmail.ru/mail/emaslov/_avatarsmall",     
        "pic_big": "http://avt.appsmail.ru/mail/emaslov/_avatarbig",
        "link": "http://my.mail.ru/mail/emaslov/",
        "referer_type": "", 
        "referer_id": "",
        "is_online": 1, 
        "friends_count": 145, 
        "is_verified": 1, 
        "vip" : 0, 
        "app_installed": 1, 
        "location": {
          "country": {
            "name": "Россия",
            "id": "24"
          },
          "city": {
            "name": "Москва",
            "id": "25"
          },
          "region": {
            "name": "Москва",
            "id": "999999"
          }
        }
      }
    ]

为此,我使用这一行:

  List<PersonInfo> pi = new JavaScriptSerializer().Deserialize<List<PersonInfo>>(friendsInfo);

friendsInfo是我创建的一个类,可以序列化JSON格式:

 public class PersonInfo
    { 
     public string uid { get; set; }
     public string first_name { get; set; }
     public string last_name { get; set; }
     public string email { get; set; }
     public string nick { get; set; }
     public string sex { get; set; }
     public string birthday { get; set; }
     public string has_pic { get; set; }
     public string pic { get; set; }
     public string pic_small { get; set; }
     public string pic_big { get; set; }
     public string link { get; set; }
     public string referer_type { get; set; }
     public string referer_id { get; set; }
     public string is_online { get; set; }
     public string is_verified { get; set; }
     public string vip { get; set; }
     public string friends_count { get; set; }


}

我不知道如何对待这部分JSON格式的问题:

             "location": {
          "country": {
            "name": "Россия",
            "id": "24"
          },
          "city": {
            "name": "Москва",
            "id": "25"
          },
          "region": {
            "name": "Москва",
            "id": "999999"
          }
        }

我的意思是我应该在PersonInfo类中创建哪些属性来响应上述内容 JSON块。

有什么想法吗?

提前谢谢。

2 个答案:

答案 0 :(得分:3)

如果查看Json2CSharp,您可以生成课程。看看这些......

public class Country
{
    public string name { get; set; }
    public string id { get; set; }
}

public class City
{
    public string name { get; set; }
    public string id { get; set; }
}

public class Region
{
    public string name { get; set; }
    public string id { get; set; }
}

public class Location
{
    public Country country { get; set; }
    public City city { get; set; }
    public Region region { get; set; }
}

public class PersonInfo
{
    public string uid { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string nick { get; set; }
    public string email { get; set; }
    public int sex { get; set; }
    public string birthday { get; set; }
    public int has_pic { get; set; }
    public string pic { get; set; }
    public string pic_small { get; set; }
    public string pic_big { get; set; }
    public string link { get; set; }
    public string referer_type { get; set; }
    public string referer_id { get; set; }
    public int is_online { get; set; }
    public int friends_count { get; set; }
    public int is_verified { get; set; }
    public int vip { get; set; }
    public int app_installed { get; set; }
    public Location location { get; set; }
}

答案 1 :(得分:1)

您可以按如下方式为所有人分开:

位置:

public class Location{ 
    public country country { get; set; }
    public city city { get; set; }
    public region region {get; set; }
}

public class Country{
    public string name { get; set; }
    public string id { get; set; }
}

public class Region{
    public string name { get; set; }
    public string id {get; set; }
}

public class City{
    public string name {get; set; }
    public string id { get; set; }
}

之后,您可以将公共位置位置插入主班级!