发行Json Object和C#

时间:2018-10-31 06:20:05

标签: c# json

我有这个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"
   }
 }

...有很多记录。

这是我的模特:

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; }        
}

这是我的控制器:

[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;

        }
    }
}

我得到这个错误:

  

JsonSerializationException:无法反序列化当前JSON对象   (例如{“ name”:“ value”})转换为类型'System.Collections.Generic.List`1'   因为该类型需要JSON数组(例如[1,2,3])进行反序列化   正确。

我认为我的问题是我没有正确处理JSON结构。 Somebady可以帮我吗?非常感谢。

3 个答案:

答案 0 :(得分:2)

这是一本字典,以下内容可能对您有用

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

答案 1 :(得分:2)

如果您像这样使用一些LINQ,请详细说明@TheGeneral给出的答案

 $this['config']->set('app.locale', $locale);
        $this['translator']->setLocale($locale);
        $this['events']->fire('locale.changed', array($locale));

 static::translator()->addResource('array', require '/var/www/html/scrumwala/vendor/nesbot/carbon/src/Carbon' . '/Lang/' . $locale . '.php', $locale);
where $locale=en

这将直接从json中返回GetInfoCoinsPoloniex对象的列表。

  

编辑:您可以接受编辑,以便我收回DVD。我不知道   我怎么会误以为我点击并没有意识到,但仍然是我的   抱歉,兄弟

答案 2 :(得分:0)

List替换为Dictionary,如下所示:

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

然后使用如下:

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