How to access elements of a JArray (or iterate over them)

前端 未结 3 1239
暗喜
暗喜 2020-12-02 22:21

I have the following Json gotten from Twitter

     +      token   {[
  {
    \"trends\": [
      {
        \"name\": \"Croke Park II\",
        \"url\": \"ht         


        
相关标签:
3条回答
  • 2020-12-02 22:34

    Once you have a JArray you can treat it just like any other Enumerable object, and using linq you can access them, check them, verify them, and select them.

    var str = @"[1, 2, 3]";
    var jArray = JArray.Parse(str);
    Console.WriteLine(String.Join("-", jArray.Where(i => (int)i > 1).Select(i => i.ToString())));
    
    0 讨论(0)
  • 2020-12-02 22:45

    There is a much simpler solution for that.
    Actually treating the items of JArray as JObject works.
    Here is an example:
    Let's say we have such array of JSON objects:

    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
                  }]");
    

    To get access each item we just do the following:

    foreach (JObject item in jArray)
    {
        string name = item.GetValue("name").ToString();
        string url = item.GetValue("url").ToString();
        // ...
    }
    
    0 讨论(0)
  • 2020-12-02 22:49

    Update - I verified the below works. Maybe the creation of your JArray isn't quite right.

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

    So call Children on your JArray to get each JObject in JArray. Call Children on each JObject to access the objects properties.

    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
    }
    
    0 讨论(0)
提交回复
热议问题