C# 使用 newtonsoft json.net 将 json 数组转换为对象列表

埃顿

我发现了类似的问题,但似乎没有任何解决方案对我的情况有帮助。

我的 json 看起来像这样:

{"array": [
    {"order_id": 4923887462, "type_id": 19297, "location_id": 60008494, "volume_total": 1, "volume_remain": 1, "min_volume": 1, "price": 130000000.0, "is_buy_order": false, "duration": 90, "issued": "2017-07-25T16:40:18Z", "range": "region"}, 
    {"order_id": 4926414947, "type_id": 19297, "location_id": 60008494, "volume_total": 1, "volume_remain": 1, "min_volume": 1, "price": 92000000.0, "is_buy_order": false, "duration": 90, "issued": "2017-07-29T06:47:29Z", "range": "region"}, 
    {"order_id": 4927013184, "type_id": 19297, "location_id": 60008494, "volume_total": 1, "volume_remain": 1, "min_volume": 1, "price": 91999989.82, "is_buy_order": false, "duration": 90, "issued": "2017-07-29T22:26:05Z", "range": "region"}, 
    {"order_id": 4927082974, "type_id": 19297, "location_id": 60008494, "volume_total": 2, "volume_remain": 2, "min_volume": 1, "price": 91999989.81, "is_buy_order": false, "duration": 90, "issued": "2017-07-30T00:22:36Z", "range": "region"}
]}

我的处理代码的相关部分如下所示:

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace sqlite_test
{
  public class Program
  {
    public static void Main()
    {
      int item = 19297;

      string json = ESIHelper.MarketFetch.getMarketData((int)ESIHelper.Regions.Domain,item,"sell",1).Result;
      ESIHelper.Models.MarketOrderList obj = JsonConvert.DeserializeObject<ESIHelper.Models.MarketOrderList>(json);
    }
  }
}

using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ESIHelper.Models
{
  public class MarketOrder
  {
    [Newtonsoft.Json.JsonProperty(PropertyName = "duration")]
    public int duration { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "is_buy_order")]
    public bool is_buy_order { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "issued")]
    public string issued { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "location_id")]
    public int location_id { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "min_volume")]
    public int min_volume { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "order_id")]
    public int order_id { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "price")]
    public double price { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "range")]
    public string range { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "type_id")]
    public int type_id { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "volume_remain")]
    public int volume_remain { get; set; }
    [Newtonsoft.Json.JsonProperty(PropertyName = "volume_total")]
    public int volume_total { get; set; }
  }

  public class MarketOrderList
  {
    public List<MarketOrder> MarketOrder { get; set; }
  }
}

在内存中,我的 json 字符串看起来像预期的那样,jsonobj为空。

我正在使用 .NET core 1.1 和 Newtonsoft Json.net 10.0.3。

布赖恩·罗杰斯

你得到的是否是空的对象,因为你没有一个添加[JsonProperty]属性添加到MarketOrder你的财产MarketOrderList类和属性名称不匹配什么是在JSON。应该是这样的:

public class MarketOrderList
{
    [JsonProperty(PropertyName = "array")]
    public List<MarketOrder> MarketOrder { get; set; }
}

但是,您还有另一个问题。order_idJSON 中值太大而无法放入int. 您需要将中的order_id属性声明更改MarketOrder为 along才能使其工作。

public class MarketOrder
{
    ...

    [JsonProperty(PropertyName = "order_id")]
    public long order_id { get; set; }

    ...
}

通过这两个更改,它将很好地反序列化。

小提琴:https : //dotnetfiddle.net/TcAfAq

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

Dart将JSON对象数组转换为对象列表

来自分类Dev

如何将Json数组转换为C#中的对象列表

来自分类Dev

如何使用json.net将复杂的json对象转换为CLR对象?

来自分类Dev

使用Newtonsoft解析JSON数组

来自分类Dev

如何使用jq将JSON对象流转换为数组

来自分类Dev

无法将类型为“ Newtonsoft.Json.Linq.JObject”的对象转换为类型为“ Newtonsoft.Json.Linq.JArray”的对象

来自分类Dev

如何使用Json.NET将JSON转换为BSON

来自分类Dev

使用NewtonSoft将JSON对象反序列化为.Net对象

来自分类Dev

将JSON数组转换为Java类对象列表

来自分类Dev

将JSON对象转换为C#列表

来自分类Dev

JSON.net:使用JsonConverter将1个对象转换为2个没有父对象的单独数组

来自分类Dev

.net Core 3.1 Newtonsoft.Json将dictionary <int,string>键转换为字符串

来自分类Dev

.net Core 3.1 Newtonsoft.Json将dictionary <int,string>键转换为字符串

来自分类Dev

将json对象数组转换为json

来自分类Dev

如何使用Newtonsoft将json字符串转换为json数组?

来自分类Dev

如何将Json文件(包含数组)转换为(类对象的)列表C#Unity

来自分类Dev

Dart将JSON对象数组转换为对象列表

来自分类Dev

使用Json.NET(Newtonsoft)将空数组从JSON转换为XML

来自分类Dev

使用JSON.NET将C#对象转换为JSON时,多余的'\'是逗号

来自分类Dev

使用属性将C#中的对象转换为JSON

来自分类Dev

使用JSON.NET将字典转换为JSON

来自分类Dev

如何将JSON数组转换为C#列表?

来自分类Dev

C#JSON Newtonsoft转换

来自分类Dev

使用jq将Json表数组转换为对象

来自分类Dev

如何使用 JSON 将 C# 对象转换为 Javascript 数组数组?

来自分类Dev

使用javascript将JSON对象转换为数组

来自分类Dev

使用 Newtonsoft 在 C# 中解析动态 JSON 数组

来自分类Dev

使用 NewtonSoft JSON 解析 JSON 数组

来自分类Dev

使用 Javascript 将 JSON 数组转换为 JSON 对象

Related 相关文章

  1. 1

    Dart将JSON对象数组转换为对象列表

  2. 2

    如何将Json数组转换为C#中的对象列表

  3. 3

    如何使用json.net将复杂的json对象转换为CLR对象?

  4. 4

    使用Newtonsoft解析JSON数组

  5. 5

    如何使用jq将JSON对象流转换为数组

  6. 6

    无法将类型为“ Newtonsoft.Json.Linq.JObject”的对象转换为类型为“ Newtonsoft.Json.Linq.JArray”的对象

  7. 7

    如何使用Json.NET将JSON转换为BSON

  8. 8

    使用NewtonSoft将JSON对象反序列化为.Net对象

  9. 9

    将JSON数组转换为Java类对象列表

  10. 10

    将JSON对象转换为C#列表

  11. 11

    JSON.net:使用JsonConverter将1个对象转换为2个没有父对象的单独数组

  12. 12

    .net Core 3.1 Newtonsoft.Json将dictionary <int,string>键转换为字符串

  13. 13

    .net Core 3.1 Newtonsoft.Json将dictionary <int,string>键转换为字符串

  14. 14

    将json对象数组转换为json

  15. 15

    如何使用Newtonsoft将json字符串转换为json数组?

  16. 16

    如何将Json文件(包含数组)转换为(类对象的)列表C#Unity

  17. 17

    Dart将JSON对象数组转换为对象列表

  18. 18

    使用Json.NET(Newtonsoft)将空数组从JSON转换为XML

  19. 19

    使用JSON.NET将C#对象转换为JSON时,多余的'\'是逗号

  20. 20

    使用属性将C#中的对象转换为JSON

  21. 21

    使用JSON.NET将字典转换为JSON

  22. 22

    如何将JSON数组转换为C#列表?

  23. 23

    C#JSON Newtonsoft转换

  24. 24

    使用jq将Json表数组转换为对象

  25. 25

    如何使用 JSON 将 C# 对象转换为 Javascript 数组数组?

  26. 26

    使用javascript将JSON对象转换为数组

  27. 27

    使用 Newtonsoft 在 C# 中解析动态 JSON 数组

  28. 28

    使用 NewtonSoft JSON 解析 JSON 数组

  29. 29

    使用 Javascript 将 JSON 数组转换为 JSON 对象

热门标签

归档