1

I have this JSON:

{
"BTC_BCN": {
    "id": 7,
    "last": "0.00000019",
    "lowestAsk": "0.00000020",
    "highestBid": "0.00000019",
    "percentChange": "0.00000000",
    "baseVolume": "7.27124323",
    "quoteVolume": "36958572.58593949",
    "isFrozen": "0",
    "high24hr": "0.00000020",
    "low24hr": "0.00000019"
},
"BTC_BTS": {
    "id": 14,
    "last": "0.00001512",
    "lowestAsk": "0.00001518",
    "highestBid": "0.00001512",
    "percentChange": "0.00000000",
    "baseVolume": "3.82925362",
    "quoteVolume": "253971.93868064",
    "isFrozen": "0",
    "high24hr": "0.00001525",
    "low24hr": "0.00001495"
   }
 }

...With a lot more records.

This is my Model:

public class GetInfoCoinsPoloniex
{

    public int id { get; set; }
    public string last { get; set; }
    public string lowestAsk { get; set; }
    public string highestBid { get; set; }
    public string percentChange { get; set; }
    public string baseVolume { get; set; }
    public string quoteVolume { get; set; }
    public string isFrozen { get; set; }
    public string high24hr { get; set; }
    public string low24hr { get; set; }
}

public class RootPoloniex
{
    public GetInfoCoinsPoloniex symbol { get; set; }        
}

And this my Controller:

[Route("api/poloniex")]
[ApiController]
public class PoloniexController : ControllerBase
{
    [HttpGet]
    public async Task<IEnumerable<GetInfoCoinsPoloniex>> GetCoinsPoloniex()
    {
        string Baseurl = "https://poloniex.com";
        string Parameters = "public?command=returnTicker";
        List<GetInfoCoinsPoloniex> CoinsInfoPoloniex = new List<GetInfoCoinsPoloniex>();            

        using (var client = new HttpClient())
        {
            //Passing service base url  
            client.BaseAddress = new Uri(Baseurl);

            client.DefaultRequestHeaders.Clear();
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            HttpResponseMessage Res = await client.GetAsync(Parameters);

            //Checking the response is successful or not which is sent using HttpClient  
            if (Res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api   
            string CoinResponse = Res.Content.ReadAsStringAsync().Result;
            CoinsInfoPoloniex = JsonConvert.DeserializeObject<List<GetInfoCoinsPoloniex>>(CoinResponse);

            }

            return CoinsInfoPoloniex;

        }
    }
}

And i get this error:

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I think my problem is that i'm not propertly handling the JSON structure. Somebady help me please?. Thanks a lot.

2
  • The JSON structure is a dictionary/object, you can’t deserialize it into an array as the error says. Try deserializing into a dictionary instead Commented Oct 31, 2018 at 6:22
  • Your json declaration is an object not an array. Replace parent parenthesis {} to [] and try Commented Oct 31, 2018 at 6:36

3 Answers 3

2

This is a dictionary, The following may work for you

CoinsInfoPoloniex = JsonConvert.DeserializeObject<Dictionary<string,GetInfoCoinsPoloniex>>(CoinResponse);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. As we say in Colombia, "you have saved the homeland." Thank you for your time.
2

Elaborating a bit more on the answer given by @TheGeneral, if you use a bit of LINQ like this,

JsonConvert.DeserializeObject<Dictionary<string,GetInfoCoinsPoloniex>>(CoinResponse).Select( o => new GetInfoCoinsPoloniex() { id = o.Key, ...}).ToList();

This will return you a list of GetInfoCoinsPoloniex objects directly from the json.

Edit: Can you accept the edit so i can take the dv back. I don't know how, i think by mistake i clicked and didn't realize and but still my apologies bro

Comments

0

Replace List with Dictionary as follows:

Dictionary<string,GetInfoCoinsPoloniex> CoinsInfoPoloniex = new Dictionary<string,GetInfoCoinsPoloniex>();

then use as follows:

CoinsInfoPoloniex = JsonConvert.DeserializeObject<Dictionary<string,GetInfoCoinsPoloniex>>(CoinResponse);

Comments

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.