• <tfoot id='V3pxv'></tfoot>

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

      <legend id='V3pxv'><style id='V3pxv'><dir id='V3pxv'><q id='V3pxv'></q></dir></style></legend>

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

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

        无法确定类型“Class"的 JSON 对象类型.

        时间:2023-05-23

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

                • <bdo id='REe4I'></bdo><ul id='REe4I'></ul>

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

                  本文介绍了无法确定类型“Class"的 JSON 对象类型.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  尝试将类类型的对象添加到 JArray 时出现以下错误.

                  I got the following error while trying to add an object of type class to the JArray.

                  无法确定类型Class"的 JSON 对象类型

                  这是我正在使用的代码:

                  Here is the code that I am using:

                  private dynamic _JArray = null
                  
                  private JArray NArray(Repository repository)
                      {
                          _JArray = new JArray();
                  
                          string[] amounts = repository.Amounts.Split('|');
                  
                          for (int i = 0; i <= amounts.Length; i++)
                          {
                              _JArray.Add(
                                  new AmountModel
                                  {
                                      Amounts = amounts[i],
                                  });
                          }
                  
                          return _JArray;
                      }
                  
                  public class AmountModel
                  {
                      public string Amounts;
                  }
                  

                  我在运行程序时这样称呼它:

                  And I call it like the following when run the program:

                  _JArray = NArray(repository);
                  
                  Console.WriteLine(JsonConvert.SerializeObject(_JArray));
                  

                  如何将_JArray (JArray)中的AmountModel (class)转换为JSON对象?

                  How can I convert the AmountModel (class) inside of _JArray (JArray), to be recognized by the system as JSON object?

                  非常感谢您的回答.

                  谢谢.

                  推荐答案

                  为了将任意非原始 POCO 添加到 JArray(或 JObject),您必须使用 JToken.FromObject() 的重载之一显式序列化它:

                  In order to add an arbitrary non-primitive POCO to a JArray (or JObject), you must explicitly serialize it, using one of the overloads of JToken.FromObject():

                  _JArray = new JArray();
                  
                  string[] amounts = repository.Amounts.Split('|');
                  
                  for (int i = 0; i < amounts.Length; i++)
                  {
                      _JArray.Add(JToken.FromObject(
                          new AmountModel
                          {
                              Amounts = amounts[i],
                          }));
                  }
                  
                  return _JArray;
                  

                  (另请注意,我更正了 for 循环中的结束条件.它是 i <= amount.Length,导致 IndexOutOfRangeException 异常.)

                  (Note also that I corrected the end condition in your for loop. It was i <= amounts.Length, which resulted in an IndexOutOfRangeException exception.)

                  工作示例 .Net fiddle #1 这里.

                  Working sample .Net fiddle #1 here.

                  或者,您可以使用 LINQ 和 JArray.FromObject 简化代码() 通过将字符串数组投影到 AmountModel 可枚举然后在一次调用中将整个序列序列化为 JArray:

                  Alternatively, you could simplify your code with LINQ and JArray.FromObject() by projecting the string array to an AmountModel enumerable then serializing the entire sequence to a JArray in one call:

                  var _JArray = JArray.FromObject(amounts.Select(a => new AmountModel { Amounts = a }));
                  

                  小提琴示例 #2 这里.

                  这篇关于无法确定类型“Class"的 JSON 对象类型.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)? 下一篇:使用 JSON 序列化/反序列化 TimeSpan

                  相关文章

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

                  • <bdo id='dIMQF'></bdo><ul id='dIMQF'></ul>

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

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