0

Как распарсить такой json?

[
    [
        1633471200000,
        "51742.81000000",
        "51792.39000000",
        "51348.00000000",
        "51375.88000000",
        "509.50068000",
        1633474799999,
        "26241395.67083850",
        19649,
        "212.80314000",
        "10960443.63736740",
        "0"
    ],
    [
        1633474800000,
        "51375.87000000",
        "51640.00000000",
        "51370.62000000",
        "51493.60000000",
        "271.34142000",
        1633478399999,
        "13983419.71127710",
        12823,
        "152.68179000",
        "7868298.05249060",
        "0"
    ],
    [
        1633478400000,
        "51498.50000000",
        "51524.99000000",
        "51224.46000000",
        "51345.81000000",
        "432.97785000",
        1633481999999,
        "22229668.86963980",
        17184,
        "215.08740000",
        "11043762.36694210",
        "0"
    ]
]

С таким проблем нет

[
    {
        "id": 1115817744,
        "price": "61990.25000000",
        "qty": "0.00255000",
        "quoteQty": "158.07513750",
        "time": 1635282032007,
        "isBuyerMaker": true,
        "isBestMatch": true
    },
    {
        "id": 1115817745,
        "price": "61990.26000000",
        "qty": "0.00805000",
        "quoteQty": "499.02159300",
        "time": 1635282032142,
        "isBuyerMaker": false,
        "isBestMatch": true
    },
    {
        "id": 1115817746,
        "price": "61990.26000000",
        "qty": "0.00391000",
        "quoteQty": "242.38191660",
        "time": 1635282032196,
        "isBuyerMaker": false,
        "isBestMatch": true
    }
]

Для первого json есть класс

public class Candle
{
    public List<object> MyArray { get; set; }
}

Для второго

public class RecentTradesList
{
    public Int64 id    { get; set; }
    public string price { get; set; }
    public string qty   { get; set; }
    public string quoteQty { get; set; }
    public Int64  time     { get; set; }
    public bool isBuyerMaker { get; set; }
    public bool isBestMatch  { get; set; }
}

Второй json без проблем разбираю с помощью

var _data = await client.GetFromJsonAsync<RecentTradesList[]>(_comand + _body);

А первый не могу с помощью

var _data = await client.GetFromJsonAsync<Candle[]>(_comand + _body);

Я понимаю что это из за того что нету name в json но как быть в таком случае?

upd:

    public class Root
    {
        public List<List<object>> MyArray { get; set; }
    }

respon = await client.GetAsync(_comand + _body);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(respon.Content.ReadAsStringAsync().Result);

Содержимое respon.Content.ReadAsStringAsync().Result

[[1633471200000,"51742.81000000","51792.39000000","51348.00000000","51375.88000000","509.50068000",1633474799999,"26241395.67083850",19649,"212.80314000","10960443.63736740","0"],[1633474800000,"51375.87000000","51640.00000000","51370.62000000","51493.60000000","271.34142000",1633478399999,"13983419.71127710",12823,"152.68179000","7868298.05249060","0"],[1633478400000,"51498.50000000","51524.99000000","51224.46000000","51345.81000000","432.97785000",1633481999999,"22229668.86963980",17184,"215.08740000","11043762.36694210","0"]]

Ошибка:

{"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'binance.classes.api+Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.\r\nTo 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.\r\nPath '', line 1, position 1."}

Решение: Помогло

var myDeserializedClass = JsonConvert.DeserializeObject<List<List<object>>>(respon.Content.ReadAsStringAsync().Result);
13
  • 1
    Это массив массивов. object[][] или List<List<object>>. / В Visual Studio есть пункт меню: Edit > Paste Special > Paste Json As Classes 26 окт 2021 в 21:36
  • Candle[] -> double[][]. Если используется System.Text.Json, то заставьте этого бездарного упрямца самостоятельно из строки в число переводить, указав в его настройках NumberHandling = JsonNumberHandling.AllowReadingFromString.
    – EvgeniyZ
    26 окт 2021 в 21:41
  • "Это массив массивов. object[][] или List<List<object>>" - не помогло
    – Sergey
    26 окт 2021 в 22:20
  • не помогло - скажите хоть как вы поняли, что не помогло, ошибка какая? или что?
    – aepot
    27 окт 2021 в 0:31
  • 1
    Помогло var myDeserializedClass = JsonConvert.DeserializeObject<List<List<object>>>(respon.Content.ReadAsStringAsync().Result); Большое спасибо!
    – Sergey
    27 окт 2021 в 7:20

1 ответ 1

2
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
public class Root
{
    public List<List<object>> MyArray { get; set; }
}
4
  • Это код думаю с сайта json2csharp.com . Он бесполезен.
    – Sergey
    27 окт 2021 в 6:10
  • @Sergey подозреваю, что это не код бесполезен, он как раз нормальный а проблема в другом. А вот в чем именно - вы качественно скрываете.
    – aepot
    27 окт 2021 в 6:57
  • Отредактировал вопрос
    – Sergey
    27 окт 2021 в 7:10
  • Можете отредактировать ответ, решение в комментах выше.
    – aepot
    27 окт 2021 в 7:57

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.