簡體   English   中英

c#newtonsoft json.net在對象內部填充json對象,結果為空值

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

我有這樣的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>"]}

名為LoginDetail.cs的類表示上面的json:

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; }
}

然后在主類上,我像這樣填充json:

LoginDetail loginDetail = new LoginDetail();

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

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

但是var a會產生空值。

如何獲取訂閱數據?

如下更改您的班級結構

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; }
}

然后我將JSON字符串中的雙引號替換為單引號(僅用於測試目的),然后使用JsonConvert.DeserializeObject<T>獲取登錄詳細信息

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); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM