8

私はJSON以下のような文字列を持っています

[{
        "attachments": [{ "comment": "communication", "comment_date_time": "2035826"} ], 
        "spent_hours": "4.00", 
        "description": ""       
    }, 
   {
        "attachments": [], 
        "spent_hours": "4.00", 
        "description": ""       
    }]

C# を使用attachmentsして文字列から属性を削除するにはどうすればよいですか。JSONJSON.netを使用しています。

4

1 に答える 1

22

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));
于 2013-05-28T06:58:39.497 に答える