简体   繁体   English

如何在 UPW ZD7EFA19FBE07D39D72FD45ADB974238714CA8DE634A7CE1D083A14FZ 中反序列化 Web 中的 JSON 数组?

[英]How to Deserialize a JSON Array from Web API in UPW C#?

I want to connect to bitbay web api, get data JSON from there and save it to a file.我想连接到 bitbay web api,从那里获取数据 JSON 并将其保存到文件中。 My JSON looks like that:我的 JSON 看起来像这样:

{
  "status": "Ok",
  "items": [
    {
      "id": "737a2935-84c9-11ea-8cdc-0242ac11000e",
      "t": "1587581134890",
      "a": "0.00926098",
      "r": "29999",
      "ty": "Buy"
    },
    {
      "id": "6c4474fa-84c9-11ea-8cdc-0242ac11000e",
      "t": "1587581122794",
      "a": "0.02475367",
      "r": "29999",
      "ty": "Buy"
    }
  ]
}

I want to get t,a,r and ty from it.我想从中得到 t,a,r 和 ty。 I've got code:我有代码:

public class TradeModel
    {
        public decimal R { get; set; }
        public decimal A { get; set; }
        public string Ty { get; set; }
        public DateTime T { get; set; }
    }

public class TradeItemModel
    {
        public TradeModel Items { get; set; }
    }

public class TradeProcessor
    {
        public static async Task<TradeModel> LoadTrades( int limit = 1 )
        {
            string url = "";

            if (limit <= 300)
            {
                url = $"https://api.bitbay.net/rest/trading/transactions/BTC-PLN?limit={ limit }";
            }
            else
            {

            }

            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    TradeItemModel trade = await response.Content.ReadAsAsync<TradeItemModel>();

                    return trade.Items;       
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
    }

After I run this code I gets an Exception: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'DemoLibrary.TradeModel' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. After I run this code I gets an Exception: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'DemoLibrary.TradeModel' because the type requires a JSON object (eg {"name":"value" }) 正确反序列化。 To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array.要修复此错误,请将 JSON 更改为 JSON object(例如 {"name":"value"})或将实现的反序列化类型接口更改为数组或数组的 ILists 接口(IColle 之类的类型)从 JSON 阵列反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。 Path 'items', line 1, position 24.”路径‘项目’,第 1 行,position 24。”

The Item in TradeItemModel class should be a collection TradeModel[] or List<TradeModel> , and you can also add Status to check it if is OK or KO: TradeItemModel class 中的Item应该是一个集合TradeModel[]List<TradeModel> ,您也可以添加Status来检查它是 OK 还是 KO:

public class TradeItemModel
{
    public string Status { get; set; }
    public List<TradeModel> Items { get; set; }
}

You must change also the method signature to:您还必须将方法签名更改为:

public static async Task<List<TradeModel>> LoadTrades( int limit = 1 )

I hope this helps you out.我希望这能够帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM