如何在C#中过滤JSON数组 [英] How to filter JSON array in C#
问题描述
在这个例子中,我在SetNavRecords数组中有2条记录。
第一个是Artikelnummer:21700,第二个是Artikelnummer:21701
每个记录都有一个OfflineVerkaufspreis数组。
重要的是我在OfflineVerkaufspreis
中的Location_Code字段。我只需要一个过滤的位置代码的空洞信息。
$ b
如何为一个位置代码选择数据,例如MH?
我使用C#和Newtonsoft类JSON解析。
我曾尝试过一些LINQ版本,但没有成功。
{SetNavRecords:[{Artikelbeschreibung:Trikot \Home \2012/2013,
Artikelbeschreibung2:weiß,
Artikelnummer:21700,
Artikelrabattgruppe:MERCH,
Gutschein:false,
MwStProduktgruppe:VOLLNEU,
OfflineVerkaufspreis:[{Allow_Line_Discount:true,
Date:2014-05-16T00:00:00,
Item_No:21700,
Location_Code:BP,
Unit_Price :
{Allow_Line_Discount:true,
Date:2014-05-16T00:00:00,
Item_No:21700 ,
Location_Code:MH,
Unit_Price:5.0
},
{Allow_Line_Discount:true,
Date 05-16T00:00:00,
Item_No:21700,
L bation_code:RY,
Unit_Price:5.0
}
]
},
{Artikelbeschreibung:Autogrammtrikot 2012/2013,
Artikelbeschreibung2:weiß,
Artikelnummer:21701,
Artikelrabattgruppe:MERCH,
Gutschein:false,
MwStProduktgruppe :VOLLNEU,
OfflineVerkaufspreis:[{Allow_Line_Discount:false,
Date:2014-05-16T00:00:00,
Item_No: 21701,
Location_Code:BP,
Unit_Price:69.99
},
{Allow_Line_Discount:false,
Date 2014-05-16T00:00:00,
Item_No:21701,
Location_Code:MH,
Unit_Price:69.99
} ,
{Allow_Line_Discount:false,
Date:2014-05-16T00:00:00,
Item_No:21701,
Location_Code :RY,
单位_Price:69.99
}
]
}
]}
这是我的问题:
pre $ var tmpResult = JObject.Parse(File.ReadAllText(FileName) );
var resultObject = tmpResult [SetNavRecords]
.Values(OfflineVerkaufspreis)
.Values< JObject>()
.Where(n => n [Location_Code ] .Value< string>()==MH);
过滤器工作正常,但tmpResult中的数据不完整。我只收到OfflineVerkaufspreis中的数据。
有什么想法吗?
谢谢!
这感觉就像一个工作,通过切换到对象的json序列化可以变得更容易一些。要将json反序列化为可以使用的对象:
RootObject obj = JsonConvert.DeserializeObject< RootObject>(jsonString);
同样,您可以使用以下命令将对象转换回json:
string jsonString = JsonConvert.SerializeObject(RootObject);
基于您在问题中提供的json的对象结构是这样的:
public class OfflineVerkaufsprei
{
public bool Allow_Line_Discount {get;组; }
public string Date {get;组; }
public string Item_No {get;组; }
public string Location_Code {get;组; }
public double Unit_Price {get;组; }
}
public class SetNavRecord
{
public string Artikelbeschreibung {get;组; }
public string Artikelbeschreibung2 {get;组; }
public string Artikelnummer {get;组; }
公共字符串Artikelrabattgruppe {get;组; }
public bool Gutschein {get;组; }
public string MwStProduktgruppe {get;组; }
public List< OfflineVerkaufsprei> OfflineVerkaufspreis {get;组; }
}
public class RootObject
{
public List< SetNavRecord> SetNavRecords {get;组; }
$ / code>
然后你可以轻松地在你的对象上迭代:
pre $
(obj.SetNavRecords中的SetNavRecord i)
{
//对记录做些事
}
I have spent a lot of time to find a solution for my problem.
In this example, I have 2 records in SetNavRecords array. The first one is "Artikelnummer" : "21700" and the second one is "Artikelnummer" : "21701"
Each record have an array "OfflineVerkaufspreis".
Important is for me the field "Location_Code" in "OfflineVerkaufspreis" I only need the hole informations for one filtered Location Code.
How can I select the data for one Location Code, for example "MH"?
I am using C# and the Newtonsoft Class for JSON parsing.
I have tried some versions with LINQ but without success.
{ "SetNavRecords" : [ { "Artikelbeschreibung" : "Trikot \"Home\" 2012/2013",
"Artikelbeschreibung2" : "weiß",
"Artikelnummer" : "21700",
"Artikelrabattgruppe" : "MERCH",
"Gutschein" : false,
"MwStProduktgruppe" : "VOLLNEU",
"OfflineVerkaufspreis" : [ { "Allow_Line_Discount" : true,
"Date" : "2014-05-16T00:00:00",
"Item_No" : "21700",
"Location_Code" : "BP",
"Unit_Price" : 5.0
},
{ "Allow_Line_Discount" : true,
"Date" : "2014-05-16T00:00:00",
"Item_No" : "21700",
"Location_Code" : "MH",
"Unit_Price" : 5.0
},
{ "Allow_Line_Discount" : true,
"Date" : "2014-05-16T00:00:00",
"Item_No" : "21700",
"Location_Code" : "RY",
"Unit_Price" : 5.0
}
]
},
{ "Artikelbeschreibung" : "Autogrammtrikot 2012/2013",
"Artikelbeschreibung2" : "weiß",
"Artikelnummer" : "21701",
"Artikelrabattgruppe" : "MERCH",
"Gutschein" : false,
"MwStProduktgruppe" : "VOLLNEU",
"OfflineVerkaufspreis" : [ { "Allow_Line_Discount" : false,
"Date" : "2014-05-16T00:00:00",
"Item_No" : "21701",
"Location_Code" : "BP",
"Unit_Price" : 69.99
},
{ "Allow_Line_Discount" : false,
"Date" : "2014-05-16T00:00:00",
"Item_No" : "21701",
"Location_Code" : "MH",
"Unit_Price" : 69.99
},
{ "Allow_Line_Discount" : false,
"Date" : "2014-05-16T00:00:00",
"Item_No" : "21701",
"Location_Code" : "RY",
"Unit_Price" : 69.99
}
]
}
] }
Here is my problem:
var tmpResult = JObject.Parse(File.ReadAllText(FileName));
var resultObject = tmpResult["SetNavRecords"]
.Values("OfflineVerkaufspreis")
.Values<JObject>()
.Where(n => n["Location_Code"].Value<string>() == "MH");
The filter works fine but the data is incomplete in tmpResult. I only get the data in "OfflineVerkaufspreis". I need the root data too.
Has anybody an idea?
Thanks!
This feels like a job that could be made a little easier by switching to serialization of the json into objects. To deserialize the json into an object you could use:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
likewise, you can turn your object back into json using:
string jsonString = JsonConvert.SerializeObject(RootObject);
The structure of your object based on the json you provided in the question is such:
public class OfflineVerkaufsprei
{
public bool Allow_Line_Discount { get; set; }
public string Date { get; set; }
public string Item_No { get; set; }
public string Location_Code { get; set; }
public double Unit_Price { get; set; }
}
public class SetNavRecord
{
public string Artikelbeschreibung { get; set; }
public string Artikelbeschreibung2 { get; set; }
public string Artikelnummer { get; set; }
public string Artikelrabattgruppe { get; set; }
public bool Gutschein { get; set; }
public string MwStProduktgruppe { get; set; }
public List<OfflineVerkaufsprei> OfflineVerkaufspreis { get; set; }
}
public class RootObject
{
public List<SetNavRecord> SetNavRecords { get; set; }
}
you can then iterate easily over your objects by saying:
for each(SetNavRecord i in obj.SetNavRecords)
{
// do something to the record
}
这篇关于如何在C#中过滤JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!