簡體   English   中英

使用Newtonsoft將復雜的JSON轉換為通用列表

[英]Convert complex JSON to Generic List using Newtonsoft

以下是一個Json:

[{
    "Result": {
        "description": "Application Security Supp Specialist",
        "code": "40000003"
    }
}, {
    "Result": {
        "description": "Gvt Cyber Intelligence Specialist",
        "code": "40001416"
    }
}, {
    "Result": {
        "description": "Gvt Record Retention Specialist",
        "code": "40001428"
    }
}]

下面是我創建的類結構,因為我需要將其填充到C#對象中。 我正在嘗試創建RulesEngineOutput的集合,並用json內容填充它。

public class RulesEngineOutput
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }
}

public class RulesEngineOutputCollection
{
    public IEnumerable<RulesEngineOutput> ProbableRoles { get; set; }
}

我正在嘗試使用以下代碼實現這一目標:

var bodyJson = JsonConvert.SerializeObject(bodyString);
RulesEngineOutputCollection result = new RulesEngineOutputCollection();
foreach (var item in bodyJson)
{
    result = JsonConvert.DeserializeObject<RulesEngineOutputCollection>(item.ToString()); 
}

但這會在該項目獲取一個字符時引發異常,我在想,我需要在循環中傳遞一個JSON對象,但我無法獲取一個。 每次我得到的都是JSON字符串。

無法將當前JSON數組(例如[1,2,3])反序列化為類型'RulesEngineOutputCollection',因為該類型需要JSON對象(例如{\\“ name \\”:\\“ value \\”})才能正確反序列化。 \\ n要解決此錯誤,請將JSON更改為JSON對象(例如{\\“ name \\”:\\“ value \\”})或將反序列化類型更改為數組或實現集合接口的類型(例如ICollection,IList ),例如可以從JSON數組反序列化的List。

問題是您在RulesEngineOutput和集合之間有一個中間對象。 您需要這樣重組對象:

public class RulesEngineOutput
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }
}

public class RulesEngineOutputResult
{
    public RulesEngineOutput Result { get; set; }
}

public class RulesEngineOutputCollection
{
    public IEnumerable<RulesEngineOutputResult> ProbableRoles { get; set; }
}

然后,當您完成此重組后,可以直接反序列化為RulesEngineOutputCollection而不是對象,然后再次進行迭代和反序列化。

result = JsonConvert.DeserializeObject<RulesEngineOutputCollection>(bodyString); 

非常感謝Max,Nathan和其他人。 所以最后我對代碼進行了一些更改,下面是為了使事情正常進行而更改的代碼:

 var jToken = JObject.Parse(responseContent);
        var bodyString = jToken.SelectToken("body");
        var bodyJson = JsonConvert.SerializeObject(bodyString);


        List<RulesEngineOutput> result = new List<RulesEngineOutput>();


        try
        {

            foreach (var item in bodyString)
            {
                var formattedItem = item.SelectToken("Result");
                var  resultItem = JsonConvert.DeserializeObject<RulesEngineOutput>(formattedItem.ToString());
                   result.Add(resultItem);
            }


        }

希望它也能幫助其他人。

正如Nathan Werry所說,您將一個對象分層到另一個對象中,因此,您無法以所需的方式反序列化數據。 但是,如果首先創建這些結果的數組,然后將其分配給ProbableRoles屬性,則可以解決此問題:

var rules = new RulesEngineOutputCollection
{
    ProbableRoles = JsonConvert.DeserializeObject<Result[]>(bodyString).Select(r => r.Data).ToList()
};

public class Result
{
    [JsonProperty("Result")]
    public RulesEngineOutput Data { get; set; }
}

其他一切保持不變。 您基本上是從結果數組中創建一個新列表。 我還可以直接分配Select()結果(不調用.ToList() ),但這可以確保對象實際上具有數據,而不僅僅是引用枚舉。

暫無
暫無

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

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