使用JSON.Net解析JSON Feed [英] Parsing JSON feed using JSON.Net

查看:749
本文介绍了使用JSON.Net解析JSON Feed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读取说明,OutcomeDateTime,Team字段,这是竞争对手数组内。我试过JSON.Net但所有的例子,我发现是相当简单的json饲料,但我的Json是相当复杂。

I want to read Description, OutcomeDateTime, Team field which is inside Competitors array. I have tried JSON.Net but all the examples I found are pretty simple json feed but mine Json is quite complex. Can anyone suggest me how can I pull that fields into my C#?

{
   "State of Origin 2014":{
      "1471137":{
         "EventID":1471137,
         "ParentEventID":1471074,
         "MainEvent":"State Of Origin Series 2014",
         "Description":"Series Score - State Of Origin Series 2014",
         "Weather":null,
         "SuspendDateTime":"2014-06-18 20:10:00.0000000",
         "OutcomeDateTime":"2014-06-18 20:10:00.0000000",
         "Competition":"State of Origin 2014",
         "Competitors":{
            "ActiveCompetitors":3,
            "Competitors":[
               {
                  "Team":"New South Wales (2 - 1)",
                  "Win":"2.15",
                  "CompetitorID":8630871,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },
               {
                  "Team":"New South Wales (3 - 0)",
                  "Win":"3.05",
                  "CompetitorID":8630870,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },
               {
                  "Team":"Queensland (2 - 1)",
                  "Win":"3.30",
                  "CompetitorID":8630869,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               }
            ],
            "TotalCompetitors":3,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      },
      "1471074":{
         "EventID":1471074,
         "ParentEventID":0,
         "MainEvent":"State Of Origin Series 2014",
         "Description":"State Of Origin Series 2014",
         "Weather":null,
         "SuspendDateTime":"2014-06-18 20:10:00.0000000",
         "OutcomeDateTime":"2014-07-09 20:10:00.0000000",
         "Competition":"State of Origin 2014",
         "Competitors":{
            "ActiveCompetitors":2,
            "Competitors":[
               {
                  "Team":"New South Wales",
                  "Win":"1.33",
                  "CompetitorID":8630484,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },
               {
                  "Team":"Queensland",
                  "Win":"3.30",
                  "CompetitorID":8630483,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               }
            ],
            "TotalCompetitors":2,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      }
   },
   "State of Origin 2014 Game 2":{
      "3608662":{
         "EventID":3608662,
         "ParentEventID":3269132,
         "MainEvent":"New South Wales v Queensland",
         "Description":"5 Point Winning Margin - New South Wales v Queensland",
         "Weather":null,
         "SuspendDateTime":"2014-07-09 20:10:00.0000000",
         "OutcomeDateTime":"2014-06-18 20:10:00.0000000",
         "Competition":"State of Origin 2014 Game 2",
         "Competitors":{
            "ActiveCompetitors":39,
            "Competitors":[
               {
                  "Team":"New South Wales 6-10",
                  "Win":"4.70",
                  "CompetitorID":19153876,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },
               {
                  "Team":"Queensland 91+",
                  "Win":"201.00",
                  "CompetitorID":19153914,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               }
            ],
            "TotalCompetitors":39,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      },
      "3608676":{
         "EventID":3608676,
         "ParentEventID":3269132,
         "MainEvent":"New South Wales v Queensland",
         "Description":"1st Half Line Betting (-2) - New South Wales v Queensland",
         "Weather":null,
         "SuspendDateTime":"2014-07-09 20:10:00.0000000",
         "OutcomeDateTime":"2014-06-18 20:10:00.0000000",
         "Competition":"State of Origin 2014 Game 2",
         "Competitors":{
            "ActiveCompetitors":3,
            "Competitors":[
               {
                  "Team":"Queensland (+2)",
                  "Win":"1.96",
                  "CompetitorID":19153964,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },
               {
                  "Team":"New South Wales (-2)",
                  "Win":"2.04",
                  "CompetitorID":19153963,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },
               {
                  "Team":"Tie",
                  "Win":"10.00",
                  "CompetitorID":19153965,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               }
            ],
            "TotalCompetitors":3,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      },
      "2205961":{
         "EventID":2205961,
         "ParentEventID":1896054,
         "MainEvent":"NRL Premiership 2014",
         "Description":"NRL 2014 - To Make Grand Final",
         "Weather":null,
         "SuspendDateTime":"2014-06-20 19:45:00.0000000",
         "OutcomeDateTime":"2014-10-05 17:00:00.0000000",
         "Competition":"NRL",
         "Competitors":{
            "ActiveCompetitors":15,
            "Competitors":[
               {
                  "Team":"Sydney Roosters",
                  "Win":"2.15",
                  "CompetitorID":13332848,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               }

            ],
            "TotalCompetitors":15,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      }
   },
   "NRL Premiership Futures":{
      "1896054":{
         "EventID":1896054,
         "ParentEventID":0,
         "MainEvent":"NRL Premiership 2014",
         "Description":"NRL Premiership 2014",
         "Weather":null,
         "SuspendDateTime":"2014-06-20 19:45:00.0000000",
         "OutcomeDateTime":"2014-10-05 18:00:00.0000000",
         "Competition":"NRL Premiership Futures",
         "Competitors":{
            "ActiveCompetitors":16,
            "Competitors":[
               {
                  "Team":"Sydney Roosters",
                  "Win":"4.50",
                  "CompetitorID":11375024,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               },

               {
                  "Team":"Newcastle Knights",
                  "Win":"301.00",
                  "CompetitorID":11375033,
                  "Eliminated":false,
                  "OrderOfEntry":0,
                  "HasWinOdds":true,
                  "AllowBets":true,
                  "IsSuspended":false,
                  "EventStatus":"Open",
                  "Position":false
               }
            ],
            "TotalCompetitors":16,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      }
   }
}


推荐答案

var json = new WebClient().DownloadString("http://4gp.tw/b036/1402972549560.txt");
var obj = JObject.Parse(json);
var query =
    from JObject ev in obj.PropertyValues()
    from JObject evid in ev.PropertyValues()
    select new
    {
        Description = (string)evid["Description"],
        OutcomeDateTime = Convert.ToDateTime((string)evid["OutcomeDateTime"]),
        Teams =
            from JObject comps in evid["Competitors"]["Competitors"]
            select (string)comps["Team"],
    };

这篇关于使用JSON.Net解析JSON Feed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆