How do I get the data of a JsonConvert.DeserializeObject (s) object to be occupied as the value of a variable?

0

I have the following example string in string within an application C #:

string json={"success":56,"msg_error":"credenciales no reconocidas"}

That I receive a web service. What I need is to take the value of "success" and assign it to a variable for later use. I have tried with

var results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results['success'];
if (id == 0){
    MessageBox.Show("OK");
}else{
    MessageBox.Show("Nada"); 
}

What is inside a button of a C # form, but the visual compiler detects an error that does not appear when looking for it. What am I doing wrong, or because I can not recognize myself?

    
asked by Czar Gutz 06.12.2016 в 02:22
source

2 answers

1

In this case, as you deserialiste the Json to a class, you can make direct use of its properties:

var results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.success;
if (id == 0){
    MessageBox.Show("OK");
} else {
    MessageBox.Show("Nada"); 
}
    
answered by 06.12.2016 в 16:58
0

Using Json.net you can do the following:

dynamic json = {"success": 56, "msg_error": "credenciales no reconocidas"}

 if (json.success == 0)
 {
     MessageBox.Show("OK");
 } else {
     MessageBox.Show(json.msg_error);
 }
    
answered by 06.12.2016 в 14:37