Сообщение Re: Как десериализовать? от 11.06.2025 21:10
Изменено 11.06.2025 21:26 Shmj
Re: Как десериализовать?
Здравствуйте, Passerby, Вы писали:
P>Как десериализовать?
P>
Ответ бесплатного GPT не подходит разве?
P>Как десериализовать?
P>
P>{
P> "0": {//кроме 0 есть и другие числа в в этой строке для других запросов. Как десериализовать? public Dictionary<string, _0> dic; не работает.
P> "name": "Binance",
P> "date_live": null,
P> "url": "https://www.binance.com"
P> },
P> "pairs": [
P> {
P> "volume": 9434765.1817967,
P> "price": 0.0587,
P> },
P> {
P> "volume": 9401647.3731512,
P> "price": 2.0366,
P> }
P>}
P>Ответ бесплатного GPT не подходит разве?
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Root
{
public List<Pair> pairs { get; set; }
[JsonExtensionData]
public Dictionary<string, JsonElement> Extra { get; set; }
}
public class Exchange
{
public string name { get; set; }
public DateTime? date_live { get; set; }
public string url { get; set; }
}
public class Pair
{
public double volume { get; set; }
public double price { get; set; }
}
class Program
{
static void Main()
{
string json = @"{
""0"": {
""name"": ""Binance"",
""date_live"": null,
""url"": ""https://www.binance.com""
},
""pairs"": [
{
""volume"": 9434765.1817967,
""price"": 0.0587
},
{
""volume"": 9401647.3731512,
""price"": 2.0366
}
]
}";
var root = JsonSerializer.Deserialize<Root>(json);
foreach (var kvp in root.Extra)
{
if (int.TryParse(kvp.Key, out _))
{
var exchange = kvp.Value.Deserialize<Exchange>();
Console.WriteLine($"Exchange: {exchange.name}, URL: {exchange.url}");
}
}
foreach (var pair in root.pairs)
{
Console.WriteLine($"Pair: volume={pair.volume}, price={pair.price}");
}
}
}Re: Как десериализовать?
Здравствуйте, Passerby, Вы писали:
P>Как десериализовать?
P>
Ответ бесплатного GPT не подходит разве?
P>Как десериализовать?
P>
P>{
P> "0": {//кроме 0 есть и другие числа в в этой строке для других запросов. Как десериализовать? public Dictionary<string, _0> dic; не работает.
P> "name": "Binance",
P> "date_live": null,
P> "url": "https://www.binance.com"
P> },
P> "pairs": [
P> {
P> "volume": 9434765.1817967,
P> "price": 0.0587,
P> },
P> {
P> "volume": 9401647.3731512,
P> "price": 2.0366,
P> }
P>}
P>Ответ бесплатного GPT не подходит разве?
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Exchange
{
public string name { get; set; }
public DateTime? date_live { get; set; }
public string url { get; set; }
public List<Pair> pairs { get; set; }
}
public class Pair
{
public double volume { get; set; }
public double price { get; set; }
}
class Program
{
static void Main()
{
string json = @"{
""0"": {
""name"": ""Binance"",
""date_live"": null,
""url"": ""https://www.binance.com"",
""pairs"": [
{
""volume"": 9434765.1817967,
""price"": 0.0587
},
{
""volume"": 9401647.3731512,
""price"": 2.0366
}
]
}
}";
var exchanges = JsonSerializer.Deserialize<Dictionary<string, Exchange>>(json);
foreach (var kvp in exchanges)
{
Console.WriteLine($"Exchange ID: {kvp.Key}");
Console.WriteLine($"Name: {kvp.Value.name}");
Console.WriteLine($"URL: {kvp.Value.url}");
Console.WriteLine("Pairs:");
foreach (var pair in kvp.Value.pairs)
Console.WriteLine($" Volume: {pair.volume}, Price: {pair.price}");
}
}
}