use*_*868 5 c# json filter json.net
我花了很多时间为我的问题找到解决方案。
在此示例中,我在SetNavRecords数组中有2条记录。第一个是“ Artikelnummer”:“ 21700”,第二个是“ Artikelnummer”:“ 21701”
每个记录都有一个数组“ OfflineVerkaufspreis”。
对我而言,重要的是“ OfflineVerkaufspreis”中的“ Location_Code”字段。我只需要一个已过滤位置代码的孔信息。
如何为一个位置代码选择数据,例如“ 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" : 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
}
]
}
] }
Run Code Online (Sandbox Code Playgroud)
这是我的问题:
var tmpResult = JObject.Parse(File.ReadAllText(FileName));
var resultObject = tmpResult["SetNavRecords"]
.Values("OfflineVerkaufspreis")
.Values<JObject>()
.Where(n => n["Location_Code"].Value<string>() == "MH");
Run Code Online (Sandbox Code Playgroud)
过滤器工作正常,但tmpResult中的数据不完整。我只在“ OfflineVerkaufspreis”中获得数据。我也需要根数据。
有人知道吗?
谢谢!
这感觉像是一项可以通过将 json 序列化为对象而变得更容易的工作。要将 json 反序列化为一个对象,您可以使用:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
Run Code Online (Sandbox Code Playgroud)
同样,您可以使用以下方法将对象重新转换为 json:
string jsonString = JsonConvert.SerializeObject(RootObject);
Run Code Online (Sandbox Code Playgroud)
基于您在问题中提供的 json 的对象结构如下:
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; }
}
Run Code Online (Sandbox Code Playgroud)
然后你可以通过说轻松地迭代你的对象:
for each(SetNavRecord i in obj.SetNavRecords)
{
// do something to the record
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14412 次 |
| 最近记录: |