请教json解析

sherrywang 2018-06-13 10:43:40
这是公众平台,会员领取会员卡的时候,根据条件去读取该会员的基本资料,公众平台返回的字串如下:
{"errcode":0,"errmsg":"ok","openid":"oWssg1mmr2dPKGXOwoRd8XXXXXXX","nickname":"sherrywang","membership_number":"105191808677","bonus":30,"sex":"UNKNOW",
"user_info":{
"common_field_list":
[{"name":"USER_FORM_INFO_FLAG_MOBILE","value":"18906006765","value_list":[]}
],
"custom_field_list":[]
},

"user_card_status":"NORMAL",
"has_active":true
}
------------------------------------------------------------------------------
"user_info"里又套了层json,将字符串转化为json的时候出错了,麻烦大神指点下。


-------------------------------------------------------
//string转化为json需要的属性
public class UserInfo
{
public string errcode{get;set;}
public string errmsg{get;set;}
public string openid{get;set;}
public string nickname{get;set;}
public string bonus{get;set;}
public string balance{get;set;}
public string sex{get;set;}
public string user_card_status{get;set;}
public List<User_Info> user_info { get; set; }
}
public class User_Info
{
public List<UserProperty> common_field_list { get; set; }
public List<UserProperty> custom_field_list { get; set; }
}
public class UserProperty
{
public string name { set; get; }
public int value { set; get; }
}

//将string转化为Json
public UserInfo GetData(string jsonStr)
{
return (UserInfo)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr, typeof(UserInfo));
}
...全文
493 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sherrywang 2018-06-13
  • 打赏
  • 举报
回复
谢谢楼上两位,多谢
本人QQ-554433626 2018-06-13
  • 打赏
  • 举报
回复
Model字段请遵循帕斯卡命名法 序列化可在字段上加属性JsonProperty 例:

    public class UserProperty
    {
        [JsonProperty(PropertyName = "name")]
        public string Name { set; get; }
        [JsonProperty(PropertyName = "value")]
        public int Value { set; get; }
    }
正怒月神 2018-06-13
  • 打赏
  • 举报
回复
复制JSON,打开vs--编辑--选择性黏贴--将json转为class
public class Rootobject
{
public int errcode { get; set; }
public string errmsg { get; set; }
public string openid { get; set; }
public string nickname { get; set; }
public string membership_number { get; set; }
public int bonus { get; set; }
public string sex { get; set; }
public User_Info user_info { get; set; }
public string user_card_status { get; set; }
public bool has_active { get; set; }
}

public class User_Info
{
public Common_Field_List[] common_field_list { get; set; }
public object[] custom_field_list { get; set; }
}

public class Common_Field_List
{
public string name { get; set; }
public string value { get; set; }
public object[] value_list { get; set; }
}
本人QQ-554433626 2018-06-13
  • 打赏
  • 举报
回复
Model改一下:

    public class UserInfo
    {
        public string errcode { get; set; }
        public string errmsg { get; set; }
        public string openid { get; set; }
        public string nickname { get; set; }
        public string bonus { get; set; }
        public string balance { get; set; }
        public string sex { get; set; }
        public string user_card_status { get; set; }
        public User_Info user_info { get; set; }
    }
    public class User_Info
    {
        public List<UserProperty> common_field_list { get; set; }
        public List<UserProperty> custom_field_list { get; set; }
    }
    public class UserProperty
    {
        public string name { set; get; }
        public long value { set; get; }
    }
sherrywang 2018-06-13
  • 打赏
  • 举报
回复
用上面定义的属性,转化为json的时候出错了,麻烦走过路过的大神帮忙看下

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧