简体   繁体   中英

c# newtonsoft json.net populate json object inside object resulting null value

I have json like this:

{"ok":true,"user_id":1,"name":"ceil bleu","name_f":"ceil","name_l":"bleu","email":"xxxx@email.com","login":"ceilbleu","subscriptions":{"1":"2037-12-31","4":"2037-12-31","5":"2037-12-31","6":"2037-12-31"},"categories":[],"groups":[],"resources":["<a href=\"http:\/\/aa.com\/member\/signup\" class=\"am-resource-link\" id=\"resource-link-link-7\">Beli\/Perpanjang Keanggotaan<\/a>","<a href=\"http:\/\/aa.com\/member\/profile\" class=\"am-resource-link\" id=\"resource-link-link-15\">Unsubscribe<\/a>"]}

and a class called LoginDetail.cs represents json above:

public class LoginDetail
{
    public bool ok { get; set; }
    public int user_id { get; set; }
    public string name { get; set; }
    public string name_f { get; set; }
    public string name_l { get; set; }
    public string email { get; set; }
    public string login { get; set; }
    public Subscriptions subscriptions { get; set; }
    public object[] categories { get; set; }
    public object[] groups { get; set; }
    public string[] resources { get; set; }
}

public class Subscriptions
{
    public string _1 { get; set; }
    public string _4 { get; set; }
    public string _5 { get; set; }
    public string _6 { get; set; }
}

Then on main class I populate the json like this:

LoginDetail loginDetail = new LoginDetail();

            Newtonsoft.Json.JsonConvert.PopulateObject(json, loginDetail);
 var a = loginDetail.subscriptions._6;

                Console.WriteLine("subs 6:" + a);

But var a is resulting a null value.

How do I get subscriptions data?

Change your class structure as below

public class LoginDetail
{
    public bool ok { get; set; }
    public int user_id { get; set; }
    public string name { get; set; }
    public string name_f { get; set; }
    public string name_l { get; set; }
    public string email { get; set; }
    public string login { get; set; }
    public Subscriptions subscriptions { get; set; }
    public object[] categories { get; set; }
    public object[] groups { get; set; }
    public string[] resources { get; set; }
}

public class Subscriptions
{
    [JsonProperty("1")]
    public string _1 { get; set; }
    [JsonProperty("4")]
    public string _4 { get; set; }
    [JsonProperty("5")]
    public string _5 { get; set; }
    [JsonProperty("6")]
    public string _6 { get; set; }
}

And then I replaced double quotes with single quotes in the JSON string (only for testing purpose) and then use JsonConvert.DeserializeObject<T> to get login detail

var json = 
        @"{'ok':true,'user_id':1,'name':'ceil bleu','name_f':'ceil','name_l':'bleu','email':'xxxx@email.com','login':'ceilbleu','subscriptions':  {'1':'2037-12-31','4':'2037-12-31','5':'2037-12-31','6':'2037-12-31'} ,'categories':[],'groups':[],'resources':['<a href=\'http://aa.com/member/signup\' class=\'am-resource-link\' id=\'resource-link-link-7\'>Beli\/Perpanjang Keanggotaan<\/a>','<a href=\'http:\/\/aa.com\/member\/profile\' class=\'am-resource-link\' id=\'resource-link-link-15\'>Unsubscribe<\/a>']}";
var loginDetail = JsonConvert.DeserializeObject<LoginDetail>(json); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM