0

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()

3
  • Can you post sample json from the API? If class definitions don't correctly match the schema of the json it won't work. I can correct them if that is the case :) Commented Dec 19, 2013 at 18:04
  • Can you share the json string ? Commented Dec 19, 2013 at 18:05
  • {"getuserstatus":{"version":"1.0.0","runtime":201.36690139771,"data":{"username":"snow","shares":{"valid":0,"invalid":0,"donate_percent":0,"is_anonymous":0},"hashrate":0,"sharerate":"0.0000","transactions":{"Credit":5592.5516587,"Debit_MP":5456.48342204,"Fee":27.96275827,"TXFee":100}}}} Commented Dec 19, 2013 at 18:05

2 Answers 2

1

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; 
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this textBox1.Text += "Username: '" + a.getuserstatus.data.shares.username + "'"; and I get Username: '' in the textbox
0

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.

2 Comments

I tried this textBox1.Text += "Username: '" + a.getuserstatus.data.shares.username + "'"; and I get Username: '' in the textbox
@MichaelPinkiePieMarshall I have a theory, are you sure you don't want a.getUserStatus.data.username? data has a username property which is set in the json example you posted. I don't see one for share, I was just basing that off your class definitions. I edited my post, I think that will work. If not I'd double check your model against the json and make sure it matches exactly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.