C# 向JSON添加新值

C# 向JSON添加新值,c#,json,json.net,C#,Json,Json.net,我得到了以下JSON: string json = @"{ 'title': ['StarWars'], 'description': {'StarWars': {'type': 'blog'}} }"; 我想向描述值添加一个新值,例如: string json = @"{ 'title': ['StarWars'], 'description': {'StarWars': {'type': 'blog', 'add

我得到了以下JSON:

    string json = @"{
      'title': ['StarWars'],
      'description': {'StarWars': {'type': 'blog'}}
    }";
我想向描述值添加一个新值,例如:

string json = @"{
  'title': ['StarWars'],
  'description': {'StarWars': {'type': 'blog', 'add': true}}
}";
StarWars inside description属性的值始终与title的值相同

如何添加新属性

我解析了JSON:

var jObject = JObject.Parse(json);
object.Property("description").AddAfterSelf(new JProperty("add", true));

我知道这就是向json添加新属性的方式,但我不确定如何向封装在另一个json中的json添加属性。

您可以使用以下代码在键入后添加新标记

var jObject=jObject.Parsejson; jObject[description]?[StarWars]?[type]?.Parent?.AddAfterSelfnew JPropertyadd,true; JObject indexer返回属性的JValue,而不是属性本身,因此您应该访问其父级以调用AddAfterSelf方法


此外,在您的代码中,变量对象的名称无效

,您可以使用以下代码在键入后添加新标记

var jObject=jObject.Parsejson; jObject[description]?[StarWars]?[type]?.Parent?.AddAfterSelfnew JPropertyadd,true; JObject indexer返回属性的JValue,而不是属性本身,因此您应该访问其父级以调用AddAfterSelf方法


另外,在您的代码中,对象是变量的无效名称

您到目前为止尝试了什么?JObject对象是无效的declarationRelated:您到目前为止尝试了什么?JObject对象是无效的declarationRelated:谢谢您的回答,我完全理解它谢谢您的回答,我完全理解它