1

I want to connect to bitbay web api, get data JSON from there and save it to a file. My JSON looks like that:

{
  "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. 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 (e.g. [1,2,3]) into type 'DemoLibrary.TradeModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'items', line 1, position 24.”

1 Answer 1

1

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:

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you :). I've got one more question how can I get an element in that list for example I have button and method Click where I create variable var tradeInfo = await TradeProcessor.LoadTrades(); and I want to display R of trademodel class in my textblock which name is price?
you're welcome, you have a collection with two TradeModel, so you want the first R or sum of R? First R value : trade.Items.First().R or sum : trade.Items.Sum(x=>x.R). if the first issue is resolved by my answer, can you please mark the answer like accepted?
Yes no problem, I'll give you +1 if I can. That's all for me
yes you can now, you need just 15 reputation to upvote. so for getting the R my response works? by using sum or first functions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.