SajjadZare SajjadZare - 5 months ago 10
JSON Question

Json.NET failes to deserialize Json to a field-only class

I want to parse a piece of JSON with Newtonsoft Json.NET

JSON:

{

"USER":{
"result_id":"0",
"result_description":"NET Connections",
"cmlog_username":[
"8118236834",
"8118236834",
"8118236834"
],
"caller_id":[
"14cc20f7b05f",
"14cc20f7b05f",
"14cc20f7b05f"
]
}

}


Class

public class USER
{
public string result_id;
public string result_description;
public string[] cmlog_username;
public string[] caller_id;
}//USER


I convert it with below code but all of property value is NULL

USER con = JsonConvert.DeserializeObject<USER>(msg);

Answer

Your deserialization class is incorrect. Putting your JSON into json2csharp.com produces:

public class USER
{
    public string result_id { get; set; }
    public string result_description { get; set; }
    public List<string> cmlog_username { get; set; }
    public List<string> caller_id { get; set; }
}

public class RootObject
{
    public USER USER { get; set; }
}

So you would need to do:

User con = JsonConvert.DeserializeObject<RootObject>(msg);

Your JSON object isn't a USER, it's an object that contains a USER.