2

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.

1
  • What things have you tried, and what results have you gotten? Commented Nov 13, 2010 at 19:53

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0
using System.Web.Script.Serialization;

var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<Dictionary<string, Dictionary<string, User>>>(theJsonString);
var theUser = deserialized["newNickInfo"]["2775040"];

2 Comments

Hi Domenic, your solution does not work. Try, I get compile error: System.Collections.Generic.Dictionary<string,System.Collections.Generic.Dictionary<string,User>>' is a 'type' but is used like a 'variable'
Yes, I forgot a closing >; try it now.
0

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.

1 Comment

Be careful when posting copy and paste boilerplate/verbatim answers (stackoverflow.com/questions/4575445/…) to multiple questions, these tend to be flagged as "spammy" by the community.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.