简体   繁体   English

C#-反序列化json

[英]C# - deserialize json

I'm trying to write a trading bot as a learning experience (don't worry, I won't use it). 我正在尝试编写一个交易机器人作为学习经验(不用担心,我不会使用它)。 I am trying to deserialize the incoming data with no luck. 我试图反序列化传入的数据,但是没有运气。 It's my first time working with json in C# but I have done so in other languages, certainly not very good at it though. 这是我第一次在C#中使用json,但是我已经用其他语言做到了,尽管当然不是很好。

I created a class that looks like this: 我创建了一个看起来像这样的类:

public class Coin
    {
        public string symbol { get; set; }
        public double price {get;set;}
    }

I am fetching and reading the data like this: 我正在这样读取和读取数据:

using (WebClient w = new WebClient())
            {
                try
                {
                var json = w.DownloadString("https://api.binance.com/api/v3/ticker/price");
                int length = json.Length;
                string newJson = json.Substring(1, length-2);
//had to create new string because having [] made it crash
                Coin coin = JsonConvert.DeserializeObject<Coin>(newJson);
                Console.Write(coin); // this does not print anything
            }catch(JsonReaderException e){}
           }

Incoming data looks like this (or just follow the link): 传入数据如下所示(或仅点击链接):

{"symbol":"ETHBTC","price":"0.07190100"},{"symbol":"LTCBTC","price":"0.01298100"}

Now, I'm trying to only get one of them, but I am getting all. 现在,我尝试仅获得其中之一,但获得全部。 First of all I'm guessing there's something wrong with my Coin class and second I don't know how to access just one of them. 首先,我猜我的Coin类存在问题,其次,我不知道如何访问其中的一个。

Thanks 谢谢

You are getting array of objects, but you are trying to deserialise into a single object. 您正在获取对象数组,但是您正在尝试反序列化为单个对象。 There is a reason for [] symbols. []符号的原因。 Don't remove them. 不要删除它们。 Instead serialise into Coin[] and then take .FirstOrDefault() from that array. 而是序列化为Coin[] ,然后从该数组中获取.FirstOrDefault()

Something like this: 像这样:

using System.Linq;

var json = w.DownloadString("https://api.binance.com/api/v3/ticker/price");
var coins = JsonConvert.DeserializeObject<Coin[]>(json);
var firstCoin = coins.FirstOrDefault();
if (firstCoin != null)
{
     Console.Write($"Symbol: {firstCoin.symbol}; Price: {firstCoin.price}");
}

I don't see anything wrong with your Coin class. 我认为您的Coin课程没有任何问题。 Secondly, what I advice you to do is to deserialize the JSON into a List of coins. 其次,我建议您将JSON反序列化为硬币列表。

List<Coin> coins = new List<Coin>();
coins = JsonConvert.DeserializeObject<List<Coin>>(newJson);

In your code you deserialized multiple Coin-objects and you wanted to store them in a variable that holds one Coin-object. 在您的代码中,您反序列化了多个硬币对象,并且想要将它们存储在一个包含一个硬币对象的变量中。 That is why I adviced to make a list of objects, so that multiple Coin-objects can be stored. 这就是为什么我建议列出一个对象列表,以便可以存储多个硬币对象的原因。

You can then get one item of the list by index. 然后,您可以按索引获得列表中的一项。

// This returns the first value
var oneItem = coins[0]

I hope this did the trick! 我希望这能解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM