Comment accéder aux éléments d'un JArray

j'ai le Json suivants obtenu à partir 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}

le problème, c'est que je ne peux accéder à aucun des éléments. J'ai essayé foreach loops et normal pour loops et ne peut jamais sembler accéder à des elemets individuels il finit toujours par Accéder à l'ensemble de la zone.

une idée de comment j'accède aux éléments individuels dans ce JSON JArray?

24
demandé sur Jimbo 2013-04-16 23:34:21

3 réponses

mise à jour-j'ai vérifié les travaux ci-dessous. Peut-être que la création de votre JArray n'est pas tout à fait correcte.

[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
        }
    }

alors appelez les enfants sur votre JArray pour obtenir chaque JObject à JArray. Appelez les enfants sur chaque objet de travail pour accéder aux propriétés des objets.

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
}
25
répondu cgotberg 2013-04-16 20:30:05

en train de traiter des éléments de JArrayJObject fonctionne très bien pour moi.

Voici un exemple:

Disons que nous avons un tel tableau d'objets 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
              }]");

pour obtenir l'accès à chaque élément nous faisons juste ce qui suit:

foreach (JObject item in jArray)
{
    string name = item.GetValue("name");
    string url = item.GetValue("url");
    // ...
}
13
répondu Just Shadow 2017-01-23 16:19:16

Une fois que vous avez un JArray vous pouvez le traiter comme n'importe quel autre objet énumérable, et en utilisant linq vous pouvez y accéder, les vérifier, les vérifier, et les sélectionner.

var str = @"[1, 2, 3]";
var jArray = JArray.Parse(str);
Console.WriteLine(String.Join("-", jArray.Where(i => (int)i > 1).Select(i => i.ToString())));
7
répondu Ghasan Al-Sakkaf 2015-07-30 02:44:43