Rocky Balboa
Rocky Balboa

Reputation: 814

Access JSON data in Windows Forms Application

I have following on click event :

private void button1_Click(object sender, EventArgs e)
{
    string usrname = textBox1.Text;
    string pass = textBox2.Text;
    Service1 ser = new Service1();
    string jay = ser.UsernamePass(usrname);

    dynamic string_json = Newtonsoft.Json.JsonConvert.DeserializeObject(jay);

    var password = string_json.login.Password.Value;

    if (pass == password)
    {
        h.Show();
    }
    MessageBox.Show("Wrong Password");
}

jay has Following value :

"{\"login\":[{\"Password\":\"admin123\"}]}"

string_json has following value :

{
    "login": [
    {
        "Password": "admin123"
    }]
}

At following line : var password = string_json.login.Password.Value;

It throws me following exception :

"'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'Password'"

Can anyone help me with it that how i access data? Even it would be useful if you tell me another way to access data.

Upvotes: 0

Views: 343

Answers (1)

Elvedin Hamzagic
Elvedin Hamzagic

Reputation: 855

As I can see, login is array, so try:

var password = string_json.login[0].Password.Value;

Upvotes: 1

Related Questions