简体   繁体   English

将JSON字符串转换为C#对象

[英]converting JSON string to C# Object

I tried to deserialize JSON string to object, but got an exception, really confused. 我试图将JSON字符串反序列化为对象,但是遇到了一个异常,确实很困惑。

"{

\"lastUpdateId\":787618462,
\"bids\":[[\"10451.90000000\",\"0.18884000\"],[\"10451.70000000\",\"0.01770200\"]],

\"asks\":[[\"10457.88000000\",\"0.17060500\"],[\"10458.13000000\",\"0.79300000\"]]

}"

and this is the needed object: 这是需要的对象:

 public class OrderBook
    {
        [JsonProperty("lastUpdateId")]
        public int lastUpdateId { get; set; }

        [JsonProperty("asks")]
        public List<OrderBookOrder> Asks { get; set; }

        [JsonProperty("bids")]
        public List<OrderBookOrder> Bids { get; set; }

        public OrderBook(List<OrderBookOrder> asks, List<OrderBookOrder> bids)
        {
            Asks = asks;
            Bids = bids;
        }
        public OrderBook()
        {

        }
    }

    public class OrderBookOrder
    {
        public decimal Price { get; set; }
        public decimal Volume { get; set; }

        public OrderBookOrder(decimal price, decimal volume)
        {
            Price = price;
            Volume = volume;
        }
    }

so then I use NewtonSoft Json to convert the string to object 所以我用NewtonSoft Json将字符串转换为对象

 public static implicit operator OrderBook(ApiResponse response)
        {
            return Utilities.ConverFromJason<OrderBook>(response);
        }

I think that problem is to parce two arrays (bids and asks) but can`t solve the problem. 我认为问题是将两个数组(出价和要价)减半,但不能解决问题。 Thanks a lot for help! 非常感谢您的帮助!

The JSON you gave must have class structure as shown 您提供的JSON必须具有如下所示的类结构

public class RootObject
{
    public int lastUpdateId { get; set; }
    public List<List<string>> bids { get; set; }
    public List<List<string>> asks { get; set; }
}

Best option is to use Newtonsoft.Json (from nuget). 最好的选择是使用Newtonsoft.Json(来自nuget)。 All you have to do is: 您要做的就是:

OrderBook ob = JsonConvert.DeserializeObject<OrderBook>(response.ToString());

Your model must be something like this: 您的模型必须是这样的:

public class OrderBook
{
    [JsonProperty("lastUpdateId")]
    public int lastUpdateId { get; set; }
    [JsonProperty("bids")]
    public List<List<string>> bids { get; set; }
    [JsonProperty("asks")]
    public List<List<string>> asks { get; set; }
}

You can get a proper model from json string here: http://json2csharp.com/ 您可以在此处从json字符串中获取合适的模型: http : //json2csharp.com/

Important! 重要! All your Json properties must have public getter and setter. 您的所有Json属性必须具有公共getter和setter。 Even if you only serialize or deserialize. 即使您仅序列化或反序列化。

To fill your OrderBookOrder object, you have to create one specific method and call it after deserialize. 要填充OrderBookOrder对象,必须创建一个特定的方法并在反序列化后调用它。 It is not possible to transform a JSon model into something different using Newtonsoft.Json. 使用Newtonsoft.Json无法将JSon模型转换为其他模型。

As per your question your model class looks like below 根据您的问题,您的模型类如下所示

public class OrderBook
{

    [JsonProperty("lastUpdateId")]
    public int lastUpdateId { get; set; }

    [JsonProperty("bids")]
    public IList<IList<string>> Bids { get; set; }

    [JsonProperty("asks")]
    public IList<IList<string>> Asks { get; set; }
}

And valid json format look like below 有效的json格式如下所示

{
"lastUpdateId": 787618462,
"bids": [
    [".90000000 ", "0.18884000 "],
    ["10451.70000000 ", "0.01770200 "]
],
"asks": [
    ["10457.88000000", "0.17060500"],
    ["10458.13000000", "0.79300000"]
]
}

You can write for DeserializeObject code 您可以为DeserializeObject代码编写

public static implicit operator OrderBook(ApiResponse response)
        {
            return JsonConvert.DeserializeObject<OrderBook>(response);
        }

如果不想用属性污染模型,也可以使用Fluent-JSON.NET。

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

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