使用Newtonsoft JSON从JSON数组中提取对象

时间:2019-12-01 14:19:54

标签: c# json json.net

我有一个像波纹管这样的JSON数据

[{
    "CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":2,\"Total\":\"4000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
    "CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
    "CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":5,\"Total\":\"2500.00\",\"IsNote\":true}]"
}]

并且我想从中提取价值并期待波纹数据

[{
    "PK_MasterCurrencyDenomID": 1,
    "CurrencyDenomination": 2000,
    "NoofCurrency": 2,
    "Total": "4000.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 2,
    "CurrencyDenomination": 500,
    "NoofCurrency": 2,
    "Total": "1000.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 3,
    "CurrencyDenomination": 200,
    "NoofCurrency": 2,
    "Total": "400.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 4,
    "CurrencyDenomination": 100,
    "NoofCurrency": 2,
    "Total": "200.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 5,
    "CurrencyDenomination": 50,
    "NoofCurrency": 2,
    "Total": "100.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 6,
    "CurrencyDenomination": 20,
    "NoofCurrency": 2,
    "Total": "40.00",
    "IsNote": true
}]

为此,我编写了波纹管代码,并认为这样做是不正确的方法,必须有一些明智的方法。请向我建议一个更好的替代方法。

JArray jsonArray = JArray.Parse(json);
List<MasterCurrencyDenomination> d = new List<MasterCurrencyDenomination>();
string strjson = string.Empty;
foreach (var item in jsonArray.Children())
{

strjson+= item["CurrencyDenomination_JSON"].ToString().Replace("[", "").Replace("]", ",");

}

d = JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>("["+strjson+"]");

2 个答案:

答案 0 :(得分:2)

如果您观察到Json,很明显它是类型Array,其中包含单个String属性CurrencyDenomination_JSON。 CurrencyDenomination_JSON的值是 JSON字符串。

您需要做的是,获取这些JSON字符串(表示为IEnumerable<string>),通过解析每个JSON字符串并对其进行序列化,从中检索JObject

var currencyArray = JArray.Parse(json).Children<JObject>()
                     .SelectMany(x=>x.Properties().Select(c=>c.Value.Value<string>()));

var result = JsonConvert.SerializeObject(currencyArray.SelectMany(x=>JArray.Parse(x)));

答案 1 :(得分:0)

我认为您可以使用以下代码来做到这一点。

requests

OR:

   List<MasterCurrencyDenomination> items = new List<MasterCurrencyDenomination>();

        JToken.Parse(myJson).ToList().ForEach(x =>
        {
            items.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(x.SelectToken("CurrencyDenomination_JSON").Value<string>()));
        });