1
我有JSON文件,我想在C#中提取某个字段名称消息的值。到目前为止,我编写了一个c#控制台应用程序,但LINQ to JSON代码不适用于WHERE条件。C#Linq to json如何提取包含特定值的属性
using (StreamReader lire = File.OpenText(@"filepath"))
{
JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(lire));
var liste = from x in o["messages"]
where x["message"].Contains("photo")
select x["message"];
foreach (var x in liste)
{
Console.WriteLine(x);
}
Console.ReadKey();
}
这里的样品的JSON FILE
{
"message": "[Debug][CollectCtrl] Location Saved",
"type": "debug",
"date": 1488152282281
},
{
"message": "[Debug]INSERT INTO photo (location_id, lat, lng, json, filename, heading) VALUES (15,0, 0,'{}', 'file:///storage/emulated/0/Pictures/IMG_20170226_163727.jpg',0)",
"type": "debug",
"date": 1488152282285
},
{
"message": "[Debug]INSERT INTO photo (location_id, lat, lng, json, filename, heading) VALUES (15,0, 0,'{}', 'file:///storage/emulated/0/Pictures/IMG_20170226_163718.jpg',0)",
"type": "debug",
"date": 1488152282289
},
{
"message": "[Debug]Photo saved.28",
"type": "debug",
"date": 1488152282439
},
,我得到了一个错误说JSON格式不正确。 – Sameer
我发布的示例位于数组命名消息的内部: –
请注意我的代码从'JObject'到'JArray'的更改。希望对你有效。 – Sameer