c# - jobject - Cómo acceder a los elementos de un JArray
jobject to jarray (3)
Actualización: verifiqué los siguientes trabajos. Tal vez la creación de su JArray no sea del todo correcta.
[TestMethod]
public void TestJson()
{
var jsonString = @"{""trends"": [
{
""name"": ""Croke Park II"",
""url"": ""http://twitter.com/search?q=%22Croke+Park+II%22"",
""promoted_content"": null,
""query"": ""%22Croke+Park+II%22"",
""events"": null
},
{
""name"": ""Siptu"",
""url"": ""http://twitter.com/search?q=Siptu"",
""promoted_content"": null,
""query"": ""Siptu"",
""events"": null
},
{
""name"": ""#HNCJ"",
""url"": ""http://twitter.com/search?q=%23HNCJ"",
""promoted_content"": null,
""query"": ""%23HNCJ"",
""events"": null
},
{
""name"": ""Boston"",
""url"": ""http://twitter.com/search?q=Boston"",
""promoted_content"": null,
""query"": ""Boston"",
""events"": null
},
{
""name"": ""#prayforboston"",
""url"": ""http://twitter.com/search?q=%23prayforboston"",
""promoted_content"": null,
""query"": ""%23prayforboston"",
""events"": null
},
{
""name"": ""#TheMrsCarterShow"",
""url"": ""http://twitter.com/search?q=%23TheMrsCarterShow"",
""promoted_content"": null,
""query"": ""%23TheMrsCarterShow"",
""events"": null
},
{
""name"": ""#Raw"",
""url"": ""http://twitter.com/search?q=%23Raw"",
""promoted_content"": null,
""query"": ""%23Raw"",
""events"": null
},
{
""name"": ""Iran"",
""url"": ""http://twitter.com/search?q=Iran"",
""promoted_content"": null,
""query"": ""Iran"",
""events"": null
},
{
""name"": ""#gaa"",
""url"": ""http://twitter.com/search?q=%23gaa"",
""promoted_content"": null,
""query"": ""gaa"",
""events"": null
},
{
""name"": ""Facebook"",
""url"": ""http://twitter.com/search?q=Facebook"",
""promoted_content"": null,
""query"": ""Facebook"",
""events"": null
}]}";
var twitterObject = JToken.Parse(jsonString);
var trendsArray = twitterObject.Children<JProperty>().FirstOrDefault(x => x.Name == "trends").Value;
foreach (var item in trendsArray.Children())
{
var itemProperties = item.Children<JProperty>();
//you could do a foreach or a linq here depending on what you need to do exactly with the value
var myElement = itemProperties.FirstOrDefault(x => x.Name == "url");
var myElementValue = myElement.Value; ////This is a JValue type
}
}
Así que llame a Children en su JArray para obtener cada JObject en JArray. Llame a Children en cada JObject para acceder a las propiedades de los objetos.
foreach(var item in yourJArray.Children())
{
var itemProperties = item.Children<JProperty>();
//you could do a foreach or a linq here depending on what you need to do exactly with the value
var myElement = itemProperties.FirstOrDefault(x => x.Name == "url");
var myElementValue = myElement.Value; ////This is a JValue type
}
Tengo el siguiente Json obtenido de Twitter
+ token {[
{
"trends": [
{
"name": "Croke Park II",
"url": "http://twitter.com/search?q=%22Croke+Park+II%22",
"promoted_content": null,
"query": "%22Croke+Park+II%22",
"events": null
},
{
"name": "#twiznight",
"url": "http://twitter.com/search?q=%23twiznight",
"promoted_content": null,
"query": "%23twiznight",
"events": null
},
{
"name": "#Phanhattan",
"url": "http://twitter.com/search?q=%23Phanhattan",
"promoted_content": null,
"query": "%23Phanhattan",
"events": null
},
{
"name": "#VinB",
"url": "http://twitter.com/search?q=%23VinB",
"promoted_content": null,
"query": "%23VinB",
"events": null
},
{
"name": "#Boston",
"url": "http://twitter.com/search?q=%23Boston",
"promoted_content": null,
"query": "%23Boston",
"events": null
},
{
"name": "#rtept",
"url": "http://twitter.com/search?q=%23rtept",
"promoted_content": null,
"query": "%23rtept",
"events": null
},
{
"name": "Facebook",
"url": "http://twitter.com/search?q=Facebook",
"promoted_content": null,
"query": "Facebook",
"events": null
},
{
"name": "Ireland",
"url": "http://twitter.com/search?q=Ireland",
"promoted_content": null,
"query": "Ireland",
"events": null
},
{
"name": "Everton",
"url": "http://twitter.com/search?q=Everton",
"promoted_content": null,
"query": "Everton",
"events": null
},
{
"name": "Twitter",
"url": "http://twitter.com/search?q=Twitter",
"promoted_content": null,
"query": "Twitter",
"events": null
}
],
"as_of": "2013-04-17T13:05:30Z",
"created_at": "2013-04-17T12:51:41Z",
"locations": [
{
"name": "Dublin",
"woeid": 560743
}
]
}
]} Newtonsoft.Json.Linq.JToken {Newtonsoft.Json.Linq.JArray}
El problema es que parece que no puedo acceder a ninguno de los elementos. He probado los bucles foreach y loops normales y nunca puedo acceder a los elementos individuales, siempre termina accediendo a toda el área.
¿Alguna idea de cómo accedo a los elementos individuales en este Json JArray?
En realidad, tratar elementos de JArray
como JObject
funciona bien para mí.
Aquí hay un ejemplo:
Digamos que tenemos esa matriz de objetos JSON:
JArray jArray = JArray.parse(@"[
{
""name"": ""Croke Park II"",
""url"": ""http://twitter.com/search?q=%22Croke+Park+II%22"",
""promoted_content"": null,
""query"": ""%22Croke+Park+II%22"",
""events"": null
},
{
""name"": ""Siptu"",
""url"": ""http://twitter.com/search?q=Siptu"",
""promoted_content"": null,
""query"": ""Siptu"",
""events"": null
}]");
Para acceder a cada elemento, solo hacemos lo siguiente:
foreach (JObject item in jArray)
{
string name = item.GetValue("name");
string url = item.GetValue("url");
// ...
}
Una vez que tiene un JArray, puede tratarlo como cualquier otro objeto Enumerable, y al usar linq puede acceder a ellos, verificarlos, verificarlos y seleccionarlos.
var str = @"[1, 2, 3]";
var jArray = JArray.Parse(str);
Console.WriteLine(String.Join("-", jArray.Where(i => (int)i > 1).Select(i => i.ToString())));