2016-12-27 233 views
0

Im新的Json和我我想從我的JObject過濾不需要的信息。 我有一個JObject如下:從Jobject刪除屬性c#

{ 
    "A": "sr", 
    "B": { 
     "B1": "some data", 
     "B2": "some data, 
     "Values": [ 
     { 
      "C1": "some info", 
      "C2": someinfo", 
      "C3": { 
      "D1": "some info", 
      "D2": [ 
       "cat", 
       "dog", 
       "fish" 
      ], 
      "D3": "some info" 
      }, 
      "C4": "some info", 
      "C5": "some info" 
     }, 
     { 
      "C1": "some info", 
      "C2": someinfo", 
      "C3": { 
      "D1": "some info", 
      "D2": [ 
       "cat", 
       "dog" 
      ], 
      "D3": "some info" 
      }, 
      "C4": "some info", 
      "C5": "some info" 
     }, 
      { 
      "C1": "some info", 
      "C2": someinfo", 
      "C3": { 
      "D1": "some info", 
      "D2": [ 
       "cat", 
       "dog" 
      ], 
      "D3": "some info" 
      }, 
      "C4": "some info", 
      "C5": "some info" 
     } 
     ] 
    } 
    } 

,我想進行掃描,如果值[I] .C3.D2不包括 「魚」,我想刪除值[I],所以我想我的新(編輯後)JObject看起來像這樣:

 { 
    "A": "sr", 
    "B": { 
     "B1": "some data", 
     "B2": "some data, 
     "Values": [ 
     { 
      "C1": "some info", 
      "C2": someinfo", 
      "C3": { 
      "D1": "some info", 
      "D2": [ 
       "cat", 
       "dog", 
       "fish" 
      ], 
      "D3": "some info" 
      }, 
      "C4": "some info", 
      "C5": "some info" 
     }, 
      { 
      "C1": "some info", 
      "C2": someinfo", 
      "C3": { 
      "D1": "some info", 
      "D2": [ 
       "cat", 
       "fish" 
      ], 
      "D3": "some info" 
      }, 
      "C4": "some info", 
      "C5": "some info" 
     } 
     ] 
    } 
    } 

什麼是最好的和最乾淨的方式呢?

+1

將您的jsons轉換爲對象列表,使用linq過濾並從對象再生成jsons。 –

+0

怎麼樣?它已經是一個對象(JObject)。 – voldemort

回答

0

樣品過濾奇數:

public class MyClass{ 
    public string A{get; set;} 
    public List<int> B{get; set;} 
} 

string js= "{"A" : "1","B" : [1,2,3,4,5,6]}"; 

MyClass obj = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<MyClass>(js); 

obj.B= obj.B.Where(t => t % 2 == 0).ToList(); 
0

有些凌亂的LINQ將做到這一點。選擇Values到一個新對象中,其中D2包含「魚」,然後替換原始對象上的Values

string jsondata = @"{ ""A"":""sr"", ""B"": { ""B1"":""some data"", ""B2"":""some data"", ""Values"": [{ ""C1"":""some info"", ""C2"":""someinfo"", ""C3"": { ""D1"":""some info"", ""D2"": [ ""cat"", ""dog"", ""fish"" ], ""D3"":""some info"" }, ""C4"":""some info"", ""C5"":""some info"" }, { ""C1"":""some info"", ""C2"":""someinfo"", ""C3"": { ""D1"":""some info"", ""D2"": [ ""cat"", ""dog"" ], ""D3"":""some info"" }, ""C4"":""some info"", ""C5"":""some info"" }, { ""C1"":""some info"", ""C2"": ""someinfo"", ""C3"": { ""D1"":""some info"", ""D2"": [ ""cat"", ""dog"" ], ""D3"":""some info"" }, ""C4"":""some info"", ""C5"":""some info"" } ] } } "; 
JObject jobj = JObject.Parse(jsondata); 
var filteredValues = jobj["B"]["Values"].Select(j => j).Where(j => j["C3"]["D2"].ToArray().Contains("fish")); 
jobj["B"]["Values"] = JToken.FromObject(filteredValues); 

Console.WriteLine(JsonConvert.SerializeObject(jobj));