[c#]如何访问 JArray 的元素

JArray 格式文件。
    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
        }
    }

参考:

http://www.itstrike.cn/Question/40c3d9b3-022e-4362-b125-29a5339ef0e1.html

http://stackoverflow.com/questions/16045569/how-to-access-elements-of-a-jarray

时间: 2024-10-27 13:01:22

[c#]如何访问 JArray 的元素的相关文章

从头认识Spring-1.16 SpEl对集合的操作(1)-建立集合以及访问集合的元素,以&lt;util:list/&gt;为例

这一章节我们来讨论一下如何建立集合以及访问集合的元素? 1.建立集合? (1)domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20; public class Cake { private String name = ""; private double size = 0; public String getName() { return name; } public void setNam

用多种方法访问字符数组元素

指针是非常灵活而强大的,不会指针的C是不完整的C,也是缺失了灵魂的C.下面是对指针的一个简单的应用,通过指针实现对字符数组元素的访问. 1 #include<stdio.h> 2 #include<string.h> 3 4 int main() 5 { 6 char str[30], *s; 7 int n, i, len; 8 9 scanf("%d ", &n); 10 for(i = 0; i < n; i++) 11 scanf(&quo

访问并修改元素、属性、内容

jQuery可以很容易修改节点的内容,并且获取节点内容,有以下方法 . text()匹配集合中的所有元素的文本合并在一起 <body> <p>where</p> <p>are</p> <p>you</p> </body> $(function(){ var content=$("p").text(); console.log(content); }); 结果为where are you.修改

jquery访问动态生成元素

最近在做留言板过程中 ,遇到一个问题:通过jq动态生成的元素无法绑定事件,上网 查资料得知,动态生成元素需要通过事件代理来实现 这里直接介绍我采用的on()方式实现 : jQuery引入了全新的事件绑定机制,on()和off()两个函数统一处理事件绑定.因为在此之前有bind(), live(), delegate()等方法来处理事件绑定,jQuery从性能优化以及方式统一方面考虑决定推出新的函数来统一事件绑定方法并且替换掉以前的方法. on(events,[selector],[data],f

JArray获取元素值

#region 获取json getaqi protected string getaqi() { string url = "http://www.pm25.in/api/querys/pm2_5.json?city=无锡&token=5j1znBVAsnSf5xQyNQyq"; string r = string.Empty; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url.ToString

vue学习(五) 访问vue内部元素或者方法

//html <div id="app"> <input type="button" value="ok" v-bind:title="msg" v-on:click="show"></div> <h1>{{msg}}</h1> //script <script> var vm = new Vue({ el:'app', data:{

如何在循环中访问list前后元素

1. 复制整个list Copying and incrementing/decrementing the copy is the only way it can be done. You can write wrapper functions to hide it (and as mentioned in answers, C++11 has std::prev/std::next which do just that (and Boost defines similar functions)

C#语言提供foreach语句,该语句提供一种简单明了的方法来循环访问数组的元素。

http://you.ctrip.com/events/manzhouli319/3576647.html http://you.ctrip.com/events/manzhouli319/3576649.html http://you.ctrip.com/events/manzhouli319/3576651.html http://you.ctrip.com/events/manzhouli319/3576653.html http://you.ctrip.com/events/manzho

访问文件的元素

假设某文件名叫nba.txt ,里面的内容如下: 在python 2.6下 >>>file=open("文件路径/nba.txt","r") >>>for i in range(5):     name=file.next     print(name) 输出结果如下: 在python 3.5下: >>>file=open("文件路径/nba.txt","r") >&