Reputation: 33
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.
Upvotes: 1
Views: 87
Reputation: 1032
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
Upvotes: 2
Reputation: 214
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);
Upvotes: 0
Reputation: 81473
This is a dictionary, The following may work for you
CoinsInfoPoloniex = JsonConvert.DeserializeObject<Dictionary<string,GetInfoCoinsPoloniex>>(CoinResponse);
Upvotes: 2