0

I have this code that calls up a PHP that returns a Json

string str = "https://url/file.php";
        string[] cID = new string[] { "act=qwjFpPXuGexZBHDJEreZrAUH&CID=", "&username=", Username, "&password=", Password };
        string str1 = string.Concat(cID);
        using (WebClient webClient = new WebClient())
        {
            webClient.Proxy = null;
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string str2 = webClient.UploadString(str, str1).Replace("\r", "").Replace("\n", "").Trim();

            JsonConvert.DeserializeObject<List<Information>>(str2);

str2 returns this Json

[{"username":"xxxxxx","email":"[email protected]","plan":"0","activationdate":"","terminationdate":""}]

Which is what I want but when i try to pass it to this class it sends nothing:

public class Information
{
    
    public string ID { get; set; }
    public string username { get; set; }
    public string email { get; set; }
    public string plan { get; set; }
    public string activationdate { get; set; }
    public string terminationdate { get; set; }

    public Information()
    {
        
    }
}

And if I try to do this

label11.Text = Information.username;

I get "null" on the label and this error: An object reference is required for the nonstatic field, method, or property.

What am I doing wrong and how can I deserialize correctly to the class to be able to show the contents of the class into the labels?.

1 Answer 1

1

You have to assign the value to a variable and use the variables property to assign the value to your label.

List<Information> myVariable = JsonConvert.DeserializeObject<List<Information>>(str2);

label11.Text = myVariable[0].username;

You cannot simply access properties of the class without first creating an instance of it.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.