栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Newtonsoft.Json将属性添加到json的指定位置

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Newtonsoft.Json将属性添加到json的指定位置

我设法解决了这个问题,在这里发布了一个解决方案,以防它可以帮助其他人。我已经意识到json中的所有控件都是组件对象/数组的一部分,因此我们可以使用

JSONPath
,请参阅链接以获取更多详细信息。以下代码可以解决问题:

public void IterateJson(JObject obj, string mandatoryFieldKey)        {JToken jTokenFoundForMandatoryField = obj.SelectToken("$..components[?(@.key == '" + mandatoryFieldKey + "')]");    //Now we convert this oken into an object so that we can add properties/objects in it if (jTokenFoundForMandatoryField is JObject jObjectForMandatoryField) {     //We check if validate already exists for this field, if it does not //exists then we add validate and required property inside the if condition     if (jObjectForMandatoryField["validate"] == null)         jObjectForMandatoryField.Add("validate", JObject.FromObject(new { required = true })); //add validate and required property     else     {         //If validate does not exists then pre comes here and //we convert the validate into a JObject using is JObject statement         if (jObjectForMandatoryField["validate"] is JObject validateObject)         {  //We need to check if required property already exists, //if it does not exists then we add it inside the if condition.  if (validateObject["required"] == null)  {      validateObject.Add("required", true); //add required property  }         }     }      }     }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418428.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号