c# - C#中如何获取JSON中的值?

标签 c# asp.net json parsing json.net

我有 json 字符串:

    {
       "data": [
          {
             "id": "100000045402409_310121622373595",
             "from": {
                "name": "Ritesh Ranjan",
                "id": "100000045402409"
             },
             "message": "greatttttttttttttt ab jaooooooooo",
             "picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQAGY5rsr5AeM5PI&w=90&h=90&url=http\u00253A\u00252F\u00252Fwww.ndtv.com\u00252Fnews\u00252Fimages\u00252Ftopstory_thumbnail\u00252FChidambaram_2G_120.jpg",
             "link": "http://www.ndtv.com/article/india/2g-scam-chidambaram-verdict-expected-shortly-huge-implications-for-govt-173168",
             "name": "2G scam: Chidambaram verdict expected shortly, huge implications for govt",
             "caption": "www.ndtv.com",
             "description": "A Delhi court handling the 2G spectrum allocation scam trial is likely to decide today whether Union Home Minister P Chidambaram should be made a co-accused in the case for allegedly allowing former Telecom Minister A Raja to gift mobile network licenses and scarce second-generation or 2G spectrum a...",
             "icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yD/r/aS8ecmYRys0.gif",
             "type": "link",
             "application": {
                "name": "Links",
                "id": "2309869772"
             },
             "created_time": "2012-02-04T11:02:22+0000",
             "updated_time": "2012-02-04T11:02:22+0000"
          },
          {
             "id": "100003303253347_132959650157476",
             "from": {
                "name": "Suman Dey",
                "id": "100003303253347"
             },
             "message": "Check out this article I was reading on biNu. 2G verdict: Chidambaram off the hook, government exhales",
             "type": "status",
             "application": {
                "name": "biNu",
                "canvas_name": "binuapp",
                "namespace": "binuapp",
                "id": "378628085054"
             },
             "created_time": "2012-02-04T10:54:19+0000",
             "updated_time": "2012-02-04T10:54:19+0000"
          },
.....
//Continued...

现在我想用c#解析它

我用过:

    WebClient client = new WebClient();
                string Json = client.DownloadString("https://graph.facebook.com/search?q=2g+verdict+Chidambaram&type=post");
                System.IO.StreamWriter SW = new System.IO.StreamWriter(JsonDestFile);
                SW.WriteLine(Json);

                System.IO.StreamWriter SW1 = new System.IO.StreamWriter(ValuesDestFile);

                JObject o = JObject.Parse(Json);
var postTitles = from p in o["data"].Children()["from"]
                             select    p["name"].Values<string>();

                        foreach (var item in postTitles)
                        {
                            SW1.WriteLine(item);
                        }
                SW1.WriteLine(name);

但我根本无法获得任何 name 值。

它给我错误:无法访问 Newtonsoft.Json.Linq.JValue 上的子值。

请建议我如何解析上述 json 的值 id, name, id (from one) , message

最佳答案

我让它工作了......

var postTitles = from p in JO["data"].Children()
                             select new
                             {
                                 Names = (string)p["from"]["name"],
                                 Msg = (string)p["message"],
                             };

使用此 LINQ 我可以访问所需的数据。

关于c# - C#中如何获取JSON中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140773/

相关文章:

c# - 是否可以更改预定义方法可以在 C# 中采用的参数?

c# - 如何关闭与 Firebird 数据库的连接

xcode - 将可变字典添加到可变字典以转换为 JSON

c# - 如何将行写入线程安全的文本文件 - 多线程

c# - 是否可以更改下方线条的颜色/文本框的边框(条目)

c# - 需要 JavaScript 语法修正

javascript - Html.BeginAjaxForm 从操作返回数据并将其注入(inject)到用户的 DOM 中

java - 验证 Jackson 中的嵌套对象

java - 使用父属性值对 jackson 进行多态反序列化

c# - 为什么我的 C# 数组在转换为对象时会丢失类型符号信息?