2013-05-28 12 views
7

ho JSON stringa come di seguitoCome rimuovere particolare attributo dalla stringa JSON utilizzando C#

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

Come posso rimuovere l'attributo attachments dalla stringa JSON utilizzando C#. Sto usando JSON.net.

+0

tenta di utilizzare Newtonsoft.JSON per convertire JSON per oggetti .NET e quindi è possibile rimuoverlo facilmente –

+1

provare questo => http: //james.newtonking.com/pages/json-net.aspx –

+0

Penso che si possa fare come descritto nel http://stackoverflow.com/a/32153051 –

risposta

18

utilizzando LINQ

var jArr = JArray.Parse(json); 

jArr.Descendants().OfType<JProperty>() 
        .Where(p => p.Name == "attachments") 
        .ToList() 
        .ForEach(att=>att.Remove()); 

var newJson = jArr.ToString(); 

o utilizzando classi anonime

var anon = new[] { new{spent_hours="", description=""} }; 
var newJson = JsonConvert.SerializeObject(
         JsonConvert.DeserializeAnonymousType(json, anon)); 
+0

funziona perfetto (utilizzando LINQ) ... –

+1

@IrappaBisanakoppa poi http://meta.stackexchange.com/questions/5234/how -contravvenga-accettare-una-risposta-lavoro – I4V

Problemi correlati