Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json String doesn't deserialize

Tags:

json

c#

json.net

I have the following JSON:

{"success":1,
    "return":{
              "funds": {
                      "usd":0,
                      "btc":0,
                      "ltc":0
              },
              "rights": {
                      "info":1,
                      "trade":0,
                      "withdraw":0
              },
              "transaction_count":1,
              "open_orders":0,
              "server_time":1406470221
              }
}

I'm trying to deserialize it with:

 JsonConvert.DeserializeObject<UserInfo>(jsonString);

Classes are as follows:

public class UserInfo
{
    [JsonProperty("transaction_count")]
    public int TransactionCount { get; set; }

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

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

    [JsonProperty("funds")]
    public Funds Funds { get; set; }

    [JsonProperty("rights")]
    public Rights Rights { get; set; }
}

public class Funds
{
    [JsonProperty("btc")]
    public decimal Btc { get; set; }

    [JsonProperty("ltc")]
    public decimal Ltc { get; set; }

    [JsonProperty("usd")]
    public decimal Usd { get; set; }

};

public class Rights
{
    [JsonProperty("info")]
    public bool Info { get; set; }
    [JsonProperty("trade")]
    public bool Trade { get; set; }
    [JsonProperty("withdraw")]
    public bool Withdraw { get; set; }
}

I tried not using the attributes and other tutorials, but nothing seems to work.. =(

Why doesn't it work? I don't know how to set return as a root, for example. Is it possible?

Thank you.

like image 333
Th3B0Y Avatar asked Apr 10 '26 15:04

Th3B0Y


2 Answers

Try this:

public class Funds
{
    public int usd { get; set; }
    public int btc { get; set; }
    public int ltc { get; set; }
}

public class Rights
{
    public int info { get; set; }
    public int trade { get; set; }
    public int withdraw { get; set; }
}

public class Return
{
    public Funds funds { get; set; }
    public Rights rights { get; set; }
    public int transaction_count { get; set; }
    public int open_orders { get; set; }
    public int server_time { get; set; }
}

public class RootObject
{
    public int success { get; set; }
    public Return @return { get; set; }
}

And to deserialize:

JsonConvert.DeserializeObject<RootObject>(jsonString);

Reference: http://json2csharp.com/

like image 167
briba Avatar answered Apr 13 '26 03:04

briba


Your JSON isn't well-formed.

{"success":1,
    "return":{
              "funds":    <-- missing {
                      "usd":0,
                      "btc":0,
                      "ltc":0
              },
              "rights":    <-- missing {
                      "info":1,
                      "trade":0,
                      "withdraw":0
              },
              "transaction_count":1,
              "open_orders":0,
              "server_time":1406470221
              }
}

Regarding your question about setting root, try writing a simple wrapper class to represent your actual JSON hierarchy, then deserialize the whole string as is, it will work if your object graph maps to the JSON.

EDIT:See @Crasher's answer, it is exactly what I mean, with an actual sample (though I didnt verify, I will upvote it), though @dcastro's answer is the cleanest.

An alternative to dcastro's exact example, if you use JObject to handle the dynamic root, you can also use JObject.First to either avoid naming the root, or allow root to change. Please upvote his answer, I'm just providing a dynamic alternative.

var root = JObject.Parse(jsonStr).First;
like image 27
codenheim Avatar answered Apr 13 '26 03:04

codenheim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!