2010-12-04 14 views
1

Ich habe json in diesem Format:Problem Deserialisierung JSON mit JSON.NET

{ 
    "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 
    } 
} 

Ich versuche, mit Eigenschaften dieses JSON-String in die Klasse deserialisieren. Klasse aussehen wie folgt:

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;} 

} 

Es funktioniert nicht. Was ist falsch?

Hier Code deserialisiert json:

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

Antwort

3

Sie benötigen einen enthaltenden Klasse wegen der minci88 Eigenschaft:

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

Und dann:

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