How do you add a JToken to an JObject?(如何将 JToken 添加到 JObject?)
问题描述
我正在尝试使用 JSON.Net 将一些文本中的 JSON 对象添加到现有 JSON 文件中.例如,如果我有如下 JSON 数据:
I'm trying to add a JSON object from some text to an existing JSON file using JSON.Net. For example if I have the JSON data as below:
  {
  "food": {
    "fruit": {
      "apple": {
        "colour": "red",
        "size": "small"
      },
      "orange": {
        "colour": "orange",
        "size": "large"
      }
    }
  }
}
我一直在尝试这样做:
var foodJsonObj = JObject.Parse(jsonText);
var bananaJson = JObject.Parse(@"{ ""banana"" : { ""colour"": ""yellow"", ""size"": ""medium""}}");
var bananaToken = bananaJson as JToken;
foodJsonObj["food"]["fruit"]["orange"].AddAfterSelf(bananaToken);
但这给出了错误:Newtonsoft.Json.Linq.JProperty cannot have multiple values."
我实际上尝试了几种不同的方法,但似乎无处可去.在我的示例中,我真正想做的是将新项目添加到水果"中.请让我知道是否有更好的方法或更简单的库来使用.
I've actually tried a few different ways but can't seem to get anywhere. In my example what I really want to do is add the new item to "fruit". Please let me know if there is a better way of doing this or a simpler library to use.
推荐答案
我想你对 JSON.Net 中什么可以容纳什么感到困惑.
I think you're getting confused about what can hold what in JSON.Net.
JToken是任何类型的 JSON 值的通用表示.它可以是字符串、对象、数组、属性等.JProperty是与名称配对的单个JToken值.只能添加到一个JObject,它的值不能是另一个JProperty.JObject是JProperties的集合.它不能直接持有任何其他类型的JToken.
- A 
JTokenis a generic representation of a JSON value of any kind. It could be a string, object, array, property, etc. - A 
JPropertyis a singleJTokenvalue paired with a name. It can only be added to aJObject, and its value cannot be anotherJProperty. - A 
JObjectis a collection ofJProperties. It cannot hold any other kind ofJTokendirectly. 
在您的代码中,您尝试将 JObject(包含香蕉"数据的那个)添加到已经有一个 JProperty(橙色")值(包含 {"colour":"orange","size":"large"} 的 JObject).如您所见,这将导致错误.
In your code, you are attempting to add a JObject (the one containing the "banana" data) to a JProperty ("orange") which already has a value (a JObject containing {"colour":"orange","size":"large"}).  As you saw, this will result in an error.
您真正想要做的是将一个名为banana"的JProperty 添加到包含其他水果JProperties 的JObject.这是修改后的代码:
What you really want to do is add a JProperty called "banana" to the JObject which contains the other fruit JProperties.  Here is the revised code:
JObject foodJsonObj = JObject.Parse(jsonText);
JObject fruits = foodJsonObj["food"]["fruit"] as JObject;
fruits.Add("banana", JObject.Parse(@"{""colour"":""yellow"",""size"":""medium""}"));
                        这篇关于如何将 JToken 添加到 JObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 JToken 添加到 JObject?
				
        
 
            
        - 使用 rss + c# 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 
						
						
						
						
						