如何从嵌套的foreach Jobject中删除属性?

时间:2018-11-29 02:28:31

标签: c# jobjectformatter

我正在将自定义模型转换为JObject,并且想要删除JObject中为null的属性。如何循环Jobject?

var serData = JsonConvert.SerializeObject(customModel);
            JObject jsonData = JObject.Parse(serData);

foreach (var opt in jsonData["parameters"])
            {
                if (((JProperty)opt).Name.ToString() == "constraints")
                {
                    //foreach (var cons in opt["constraints"])
                    foreach (var cons in ((JProperty)opt)["constraints"])
                    {
                        if (((JProperty)cons).Name.ToString() == "price_constraints")
                        {
                            foreach (var price in cons["price_constraints"])
                            {
                                if(price["price_level_3"] == null){
                                    //delete;
                                }    
                            }
                        }
                    }
                }
            }


{
    "parameters":{
        "constraints": {
          "minimum": 5,
          "minimum": 7
          "price_constraints": {
            "price_level_1": {
              "price": 0.0,
            },
            "price_level_2": {
              "price": 55.0,
            },
            "price_level_3": null,
            "price_level_4": null,    
          }             
        }
    }
}

我正在将自定义模型转换为JObject,并且想要删除JObject中为null的属性。如何循环Jobject?

0 个答案:

没有答案