Alexander Man
Alexander Man

Reputation: 63

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!

Upvotes: 0

Views: 194

Answers (4)

Antonio Rodr&#237;guez
Antonio Rodr&#237;guez

Reputation: 1126

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.

Upvotes: 1

Alev Ryustemov
Alev Ryustemov

Reputation: 150

You can also use Fluent-JSON.NET if you do not want to pollute your models with attributes.

Upvotes: 0

Kalpesh Boghara
Kalpesh Boghara

Reputation: 461

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);
        }

Upvotes: 0

Suhel Patel
Suhel Patel

Reputation: 241

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; }
}

Upvotes: 3

Related Questions