2

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.

1
  • according to jsonlint.com that's not a valid json Commented Jul 27, 2014 at 14:46

3 Answers 3

4

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/

Sign up to request clarification or add additional context in comments.

Comments

4

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;

2 Comments

sorry, I forgot them, but the original has the brackets. I've just edited the post.
I edited my response. See Crasher's answer now. I didn't verify it, but it is the general idea.
3

You don't need to create a wrapper/root class just for this, as others answers suggest.

You can parse the whole thing into a JObject, and then convert just the return node into a UserInfo object.

JObject obj = JObject.Parse(jsonStr);
var userInfo = obj["return"].ToObject<UserInfo>();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.