【问题标题】:Problem with deserialization JSON with JSON.NET使用 JSON.NET 反序列化 JSON 的问题
【发布时间】:2010-12-04 15:29:05
【问题描述】:

我有这种格式的json:

{
    "minci88": {
        "idUser": 2775040,
        "nick": "minci88",
        "photo": "1",
        "sex": 2,
        "photoAlbums": 3,
        "videoAlbums": 0,
        "sefNick": "minci88",
        "profilPercent": 92,
        "emphasis": false,
        "age": 20,
        "isBlocked": false,
        "PHOTO": {
            "normal": "PARAM_p213.215.107.125\/fotky\/277\/50\/n_2775040PARAM_ext?v=4",
            "medium": "PARAM_p213.215.107.125\/fotky\/277\/50\/m_2775040PARAM_ext?v=4",
            "24x24": "PARAM_p213.215.107.125\/fotky\/277\/50\/s_2775040PARAM_ext?v=4"
        },
        "PLUS": {
            "active": false,
            "activeTo": "2010-10-12"
        },
        "LOCATION": {
            "idRegion": "6",
            "regionName": "Tren\u010diansky kraj",
            "idCity": "139",
            "cityName": "Tren\u010d\u00edn"
        },
        "STATUS": {
            "isLoged": false,
            "isChating": false,
            "idChat": 0,
            "roomName": "",
            "lastLogin": 1291417469
        },
        "PROJECT_STATUS": {
            "photoAlbums": 3,
            "photoAlbumsFavs": 0,
            "videoAlbums": 0,
            "videoAlbumsFavs": 0,
            "videoAlbumsExts": 0,
            "blogPosts": 0,
            "emailNew": 1,
            "postaNew": 0,
            "clubInvitations": 1,
            "dashboardItems": 0
        },
        "STATUS_MESSAGE": {
            "statusMessage": "V\u0161etko dobr\u00e9 na svete je bu\u010f nemor\u00e1lne, protiz\u00e1konn\u00e9, alebo sa z toho priber\u00e1.",
            "addTime": "1284245770"
        },
        "isFriend": false,
        "isIamFriend": false
    }
}

我尝试将此 json 字符串反序列化为具有属性的类。类看起来像这样:

public class BasicData
{

    [JsonProperty("idUser")]
    public string IdUser{ get; set;}

    [JsonProperty("nick")]
    public string Nick{ get; set;}

    [JsonProperty("photo")]
    public string WithPhoto{ get; set;}

    [JsonProperty("sex")]
    public int Sex{ get; set;}

    //[JsonProperty("photoAlbums")]
    //public int NumberOfPhotoAlbums{ get; set;}

    //[JsonProperty("videoAlbums")]
    //public int NumberOfVideoAlbums{ get; set;}

    [JsonProperty("sefNick")]
    public string SefNick{ get; set;}

    [JsonProperty("profilePercent")]
    public int ProfilePercent{ get; set;}

    [JsonProperty("emphasis")]
    public bool Emphasis{ get; set;}

    [JsonProperty("age")]
    public int Age{ get; set;}

    [JsonProperty("isBlocked")]
    public bool IsBlocked{ get; set;}

    [JsonProperty("normal")]
    public string Normal { get; set; }

    [JsonProperty("medium")]
    public string Medium { get; set; }

    [JsonProperty("24x24")]
    public string Small { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("activeTo")]
    public string ActiveTo { get; set; }

    [JsonProperty("idRegion")]
    public int IdRegion { get; set; }

    [JsonProperty("regionName")]
    public string RegionName { get; set; }

    [JsonProperty("idCity")]
    public int IdCity{ get; set; }

    [JsonProperty("cityName")]
    public int CityName{ get; set; }

    [JsonProperty("isLoged")]
    public bool IsLogged{ get; set;}

    [JsonProperty("isChating")]
    public bool IsChating{ get; set;}

    [JsonProperty( "idChat")]
    public int IdChat{ get; set;}

    [JsonProperty( "roomName")]
    public string RoomNane{ get; set;}

    [JsonProperty( "lastlogin")]
    public string LastLogin{ get; set;}

    [JsonProperty( "photoAlbums")]
    public int PhotoAlbums{ get; set;}

    [JsonProperty( "photoAlbumsFavs")]
    public int PhotoAlbumsFavs{ get; set;}

    [JsonProperty( "videoAlbums")]
    public int VideoAlbums{ get; set;}

    [JsonProperty( "videoAlbumsFavs")]
    public int VideoAlbumsFavs{ get; set;}

    [JsonProperty( "videoAlbumsExts")]
    public int VideoAlbumsExts{ get; set;}

    [JsonProperty( "blogPosts")]
    public int BlogPosts{ get; set;}

    [JsonProperty( "emailNew")]
    public int EmailNew{ get; set;}

    [JsonProperty( "PostaNew")]
    public int PostaNew{ get; set;}

    [JsonProperty( "clubInvitations")]
    public int ClubInvitations{ get; set;}

    [JsonProperty( "dashboardItems")]
    public int DashboardItems{ get; set;}

    [JsonProperty( "statusMessage")]
    public string StatusMessage{ get; set;}

    [JsonProperty( "addTime")]
    public string AddTime{ get; set;}

    [JsonProperty("isFriend")]
    public bool IsFriend{ get; set;}

    [JsonProperty("isIamFriend")]
    public bool IsIamFriend{ get; set;}

}

它不起作用。怎么了?

这是反序列化 json 的代码:

var user = JsonConvert.DeserializeObject<BasicData>(res);

【问题讨论】:

    标签: json json.net


    【解决方案1】:

    由于minci88 属性,您需要一个包含类:

    public class Container
    {
        [JsonProperty("minci88")]
        public BasicData Data { get; set; }
    }
    

    然后:

    var user = JsonConvert.DeserializeObject<Container>(res).Data;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多