我有JSON
字符串,如下所示
[{
"attachments": [{ "comment": "communication", "comment_date_time": "2035826"} ],
"spent_hours": "4.00",
"description": ""
},
{
"attachments": [],
"spent_hours": "4.00",
"description": ""
}]
如何使用C#从attachments
字符串中删除JSON
属性。我正在使用JSON.net。
答案 0 :(得分:20)
使用Linq
var jArr = JArray.Parse(json);
jArr.Descendants().OfType<JProperty>()
.Where(p => p.Name == "attachments")
.ToList()
.ForEach(att=>att.Remove());
var newJson = jArr.ToString();
或使用匿名类
var anon = new[] { new{spent_hours="", description=""} };
var newJson = JsonConvert.SerializeObject(
JsonConvert.DeserializeAnonymousType(json, anon));