简体   繁体   中英

converting JSON string to C# Object

I tried to deserialize JSON string to object, but got an exception, really confused.

"{

\"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

 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

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). 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/

Important! All your Json properties must have public getter and 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. It is not possible to transform a JSon model into something different using Newtonsoft.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

{
"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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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