2

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);
2
  • Use properties, not fields. Properties are part of a class' interface while fields are meant to hold internal data only. All serializers work with properties. Fields are always treated as exceptions Commented Jun 9, 2016 at 14:22
  • 1
    @PanagiotisKanavos, JSON.NET will happily deserialize into public fields. Here's a fiddle showing it working (dotnetfiddle.net/NNEFMm). Commented Jun 9, 2016 at 14:30

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Upvote, very well explained much better than my answer posted at the same time.
The main difference is that the OP uses fields, while this answer uses properties. While it is possible to deserialize to fields, it's an exceptional case that should be avoided
@PanagiotisKanavos: While it's true that it's more academically correct to use properties it isn't the end of the world if he chooses to use fields.
@CraigW. on the contrary, it is a serious issue. Properties are part of the class's contract. Fields are not, which is why you can't define fields in an interface. Serializers and ORMs expect to find properties, not fields. They require extra configuration to use fields
0

It's because the JSON object you are trying to parse into a user is an object that has a property of 'user' that is a user object. That probably didn't make much sense. You could change your json to be

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

and it will work.

Comments

0

Try adding the get and set to your class

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

3 Comments

These aren't modifiers. The OP uses fields while you show properties. They are different kinds of members, just like a method is different from a field or a property
Thanks for the clarification @PanagiotisKanavos
@JackMorton: That's not the problem. JSON.NET will deserialize into public fields (dotnetfiddle.net/NNEFMm).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.