json.net ¿tiene método clave?


Si mi respuesta tiene clave "error", necesito procesar el error y mostrar el cuadro de advertencia.

¿Existe el método "haskey" en json.net? Como:

var x= JObject.Parse(string_my);
if(x.HasKey["error_msg"])
    MessageBox.Show("Error!")
Author: SevenDays, 2011-08-27

2 answers

Simplemente use x["error_msg"]. Si la propiedad no existe, devuelve null.

 190
Author: svick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-27 19:49:02

JObject implementa IDictionary<string, JToken>, por lo que puede utilizar:

IDictionary<string, JToken> dictionary = x;
if (dictionary.ContainsKey("error_msg"))

... o podrías usar TryGetValue. Implementa ambos métodos usando una implementación de interfaz explícita, por lo que no puede usarlos sin convertir primero a IDictionary<string, JToken>.

 84
Author: Jon Skeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-27 19:48:06