Remove specific properties from JSON object

后端 未结 2 910
孤城傲影
孤城傲影 2020-12-10 09:33

I have a JSON:

{
    \"scbs_currentstatus\": \"\",
      \"scbs_primaryissue\": \"\",
      \"_umb_id\": \"Test\",
      \"_umb_creator\": \"Admin\",
      \         


        
2条回答
  •  春和景丽
    2020-12-10 09:59

    You can parse the string first:

    var temp =  JArray.Parse(json);
    temp.Descendants()
        .OfType()
        .Where(attr => attr.Name.StartsWith("_umb_"))
        .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
        .ForEach(attr => attr.Remove()); // removing unwanted attributes
    json = temp.ToString(); // backing result to json
    

    UPDATE OR:

    result.Properties()
        .Where(attr => attr.Name.StartsWith("_umb_"))
        .ToList()
        .ForEach(attr => attr.Remove());
    

    UPDATE #2

    You can specify more conditions in where clause:

    .Where(attr => attr.Name.StartsWith("_umb_") && some_other_condition)
    

    OR

    .Where(attr => attr.Name.StartsWith("_umb_") || some_other_condition)
    

    Or whatever you need.

提交回复
热议问题