<bdo id='GoK9z'></bdo><ul id='GoK9z'></ul>

  1. <legend id='GoK9z'><style id='GoK9z'><dir id='GoK9z'><q id='GoK9z'></q></dir></style></legend>

      <tfoot id='GoK9z'></tfoot>
    1. <i id='GoK9z'><tr id='GoK9z'><dt id='GoK9z'><q id='GoK9z'><span id='GoK9z'><b id='GoK9z'><form id='GoK9z'><ins id='GoK9z'></ins><ul id='GoK9z'></ul><sub id='GoK9z'></sub></form><legend id='GoK9z'></legend><bdo id='GoK9z'><pre id='GoK9z'><center id='GoK9z'></center></pre></bdo></b><th id='GoK9z'></th></span></q></dt></tr></i><div id='GoK9z'><tfoot id='GoK9z'></tfoot><dl id='GoK9z'><fieldset id='GoK9z'></fieldset></dl></div>
    2. <small id='GoK9z'></small><noframes id='GoK9z'>

      如何将 JToken 添加到 JObject?

      时间:2023-08-22

          <small id='Lx82g'></small><noframes id='Lx82g'>

        • <i id='Lx82g'><tr id='Lx82g'><dt id='Lx82g'><q id='Lx82g'><span id='Lx82g'><b id='Lx82g'><form id='Lx82g'><ins id='Lx82g'></ins><ul id='Lx82g'></ul><sub id='Lx82g'></sub></form><legend id='Lx82g'></legend><bdo id='Lx82g'><pre id='Lx82g'><center id='Lx82g'></center></pre></bdo></b><th id='Lx82g'></th></span></q></dt></tr></i><div id='Lx82g'><tfoot id='Lx82g'></tfoot><dl id='Lx82g'><fieldset id='Lx82g'></fieldset></dl></div>
          1. <legend id='Lx82g'><style id='Lx82g'><dir id='Lx82g'><q id='Lx82g'></q></dir></style></legend>
                <tbody id='Lx82g'></tbody>
              <tfoot id='Lx82g'></tfoot>

                <bdo id='Lx82g'></bdo><ul id='Lx82g'></ul>
                本文介绍了如何将 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.
                • JObjectJProperties 的集合.它不能直接持有任何其他类型的 JToken.
                • A JToken is a generic representation of a JSON value of any kind. It could be a string, object, array, property, etc.
                • A JProperty is a single JToken value paired with a name. It can only be added to a JObject, and its value cannot be another JProperty.
                • A JObject is a collection of JProperties. It cannot hold any other kind of JToken directly.

                在您的代码中,您尝试将 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 添加到包含其他水果JPropertiesJObject.这是修改后的代码:

                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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从流中加载多个连接的 JSON 对象 下一篇:NewtonSoft.Json 具有 IEnumerable&lt;ISomeInterface&

                相关文章

                最新文章

              • <legend id='KDsXC'><style id='KDsXC'><dir id='KDsXC'><q id='KDsXC'></q></dir></style></legend>
                • <bdo id='KDsXC'></bdo><ul id='KDsXC'></ul>

                1. <i id='KDsXC'><tr id='KDsXC'><dt id='KDsXC'><q id='KDsXC'><span id='KDsXC'><b id='KDsXC'><form id='KDsXC'><ins id='KDsXC'></ins><ul id='KDsXC'></ul><sub id='KDsXC'></sub></form><legend id='KDsXC'></legend><bdo id='KDsXC'><pre id='KDsXC'><center id='KDsXC'></center></pre></bdo></b><th id='KDsXC'></th></span></q></dt></tr></i><div id='KDsXC'><tfoot id='KDsXC'></tfoot><dl id='KDsXC'><fieldset id='KDsXC'></fieldset></dl></div>

                2. <small id='KDsXC'></small><noframes id='KDsXC'>

                  1. <tfoot id='KDsXC'></tfoot>