¿Cómo enumero a través de un proyecto de trabajo?


Estoy tratando de determinar cómo acceder a los datos que están en mi proyecto de trabajo y no puedo determinar cómo usarlo.

JObject Object = (JObject)Response.Data["my_key"];

Puedo imprimirlo en la consola haciendo Consola.WriteLine (Object) y veo los datos, se ve como:

{
 "my_data" : "more of my string data"
...
}

Pero no tengo idea de cómo iterar/enumerar a través de él, ¿alguien tiene alguna idea? Estoy tan perdida ahora mismo.

Author: svick, 2012-05-11

3 answers

Si nos fijamos en la documentación para JObject, verás que implementa IEnumerable<KeyValuePair<string, JToken>>. Por lo tanto, puede iterar sobre él simplemente usando un foreach:

foreach (var x in obj)
{
    string name = x.Key;
    JToken value = x.Value;
    …
}
 123
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
2012-05-10 23:35:38

JObjects se pueden enumerar a través de objetos JProperty fundiéndolo en un JToken :

foreach (JProperty x in (JToken)obj) { // if 'obj' is a JObject
    string name = x.Name;
    JToken value = x.Value;
}

Si tienes un JObject anidado dentro de otro JObject, no necesitas lanzar porque el accessor devolverá un JToken:

foreach (JProperty x in obj["otherObject"]) { // Where 'obj' and 'obj["otherObject"]' are both JObjects
    string name = x.Name;
    JToken value = x.Value;
}
 31
Author: Daniel,
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
2017-03-22 18:39:33

La respuesta no funcionó para mí. No sé cómo consiguió tantos votos. Aunque ayudó a señalarme en una dirección.

Esta es la respuesta que funcionó para mí:

foreach (var x in jobj)
{
    var key = ((JProperty) (x)).Name;
    var jvalue = ((JProperty)(x)).Value ;
}
 9
Author: jaxxbo,
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
2015-12-31 00:27:31