Michael Marshall
Michael Marshall

Reputation: 29

having trouble understanding how to use json.net

I've been reading for hours and still can't figure out how to use this under my button I have

        WebClient wc = new WebClient();
        Uri uri = new Uri("url");
        string json = wc.DownloadString(uri);
        var a = JsonConvert.DeserializeObject<RootObject>(json);

and the classes

    public class Shares
    {
        public int valid { get; set; }
        public int invalid { get; set; }
        public int id { get; set; }
        public int donate_percent { get; set; }
        public int is_anonymous { get; set; }
        public string username { get; set; }
    }

    public class Transactions
    {
        public double Credit { get; set; }
        public double Debit_MP { get; set; }
        public double Fee { get; set; }
        public int TXFee { get; set; }
    }

    public class Data
    {
        public string username { get; set; }
        public Shares shares { get; set; }
        public int hashrate { get; set; }
        public string sharerate { get; set; }
        public Transactions transactions { get; set; }
    }

    public class Getuserstatus
    {
        public string version { get; set; }
        public double runtime { get; set; }
        public Data data { get; set; }
    }

    public class RootObject
    {
        public Getuserstatus getuserstatus { get; set; }
    }

I did a messagebox to check the string from the URL and all the data is there but it doesn't put it into the fields when I call them they're all empty

        Shares Share = new Shares();
        textBox1.Text += "Username: " + Share.username;

edit: ok I got it working the a.getsuerstatus.data.shares.username is not valid in the api I don't know why jsontocsharp site added it but all the data is there username is actually a.getuserstatus.data.username thank you all for the help or I would have still be trying to call data using Shares Share = new Shares()

Upvotes: 0

Views: 230

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48114

The problem isn't the deserialization, it's this;

   Shares Share = new Shares();
    textBox1.Text += "Username: " + Share.username;

which instead needs to be

    textBox1.Text += "Username: " + a.getUserStatus.data.username;

a is the object instantiated by json.NET. You need to go through all the indirection to access the value of username, not just allocated a new Share instance and access it's username, the two references point to entirely different objects.

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55760

The following code:

Shares Share = new Shares();
textBox1.Text += "Username: " + Share.username;

creates a new instance of an object of type Shares which has the username property initialized to an empty string.

There is no connection with the json-formatted text you'd like to parse.

The method that you are using to parse the json string: var a = JsonConvert.DeserializeObject<RootObject>(json); expects to generate an object of type RootObject

You will want to set the Text property of textbox1 equal to the corresponding Shares object that is deserialized into a.

 textbox1.Text = a.getUserStatus.data.shares.username;

Upvotes: 1

Related Questions