Reputation: 45
I am helpless, I need deserialize JSON string in this format to class.
JSON string:
{
"newNickInfo": {
"2775040": {
"idUser": 2775040,
"nick": "minci88",
"sefNick": "minci88",
"sex": 2,
"photon": "http:\/\/213.215.107.125\/fotky\/277\/50\/n_2775040.jpg?v=4",
"photos": "http:\/\/213.215.107.125\/fotky\/277\/50\/s_2775040.jpg?v=4",
"logged": false,
"idChat": 0,
"roomName": "",
"updated": 1289670130
}
}
}
Class:
public class User
{
public string idUser { get; set; }
public string nick { get; set; }
public string sefNick { get; set; }
public string sex { get; set; }
public string photon { get; set; }
public string photos { get; set; }
public bool logged { get; set; }
public int idChat { get; set; }
public string roomName { get; set; }
public string updated { get; set; }
}
First problem is, class properties must be lower case, and second problem is I try many ways but I don’t know how cut json object from this json string and deserialize in class. Any ides? Thank for any help.
One more question, what format would have c# class, if I want deserialize this json string into it.
Upvotes: 2
Views: 1938
Reputation: 3748
I use this tool Jsonclassgenerator. It could generate C# classes for the input Json string. You dont have to use the exact class generated. But you can see the format of the class and create a similar one in your project.
Upvotes: 0
Reputation: 112937
using System.Web.Script.Serialization;
var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<Dictionary<string, Dictionary<string, User>>>(theJsonString);
var theUser = deserialized["newNickInfo"]["2775040"];
Upvotes: 0
Reputation: 18812
looks like your JSON string is not correct. Look at this
string json = @"[
{
"newNickInfo": "2775040",
"idUser": "2775040",
"nick":"minci88",
"sefNick":"minci88",
"sex":2,
"photon":"http:\/\/213.215.107.125\/fotky\/277\/50\/n_2775040.jpg?v=4",
"photos":"http:\/\/213.215.107.125\/fotky\/277\/50\/s_2775040.jpg?v=4",
"logged":false,
"idChat":0,
"roomName":"",
"updated":"1289670130"
}
]";
public class User
{
[JsonProperty]
public string idUser{get;set;}
[JsonProperty]
public string nick{get;set;}
[JsonProperty]
public string sefNick{get;set;}
[JsonProperty]
public string sex{get;set;}
[JsonProperty]
public string photon{get;set;}
[JsonProperty]
public string photos{get;set;}
[JsonProperty]
public bool logged{get;set;}
[JsonProperty]
public int idChat{get;set;}
[JsonProperty]
public string roomName{get;set;}
[JsonProperty]
public string updated{get;set;}
}
List<User> users = JsonConvert.DeserializeObject<List<User>>(json);
User u1 = users[0];
Console.WriteLine(u1.nick);
Upvotes: 1