使用JSON.Net解析JSON提要



我想读取Description, OutcomeDateTime, Team字段,这是在竞争者数组内。我尝试过JSON。但我发现的所有例子都是相当简单的json feed,但我的json相当复杂。有人能建议我如何把字段拉到我的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"],
    };

下面的代码将显示所有必需的字段:

var parsedJson = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, dynamic>>>(json);
foreach (var element in parsedJson)
{
    Console.WriteLine(element.Key);
    foreach (var item in element.Value)
    {
        Console.WriteLine(item.Key);
        Console.WriteLine(item.Value.Description);
        Console.WriteLine(item.Value.OutcomeDateTime);
        IEnumerable<dynamic> competitors = item.Value.Competitors.Competitors;
        Console.WriteLine(string.Join(",", competitors.Select(x => x.Team)));
    }
}

在dotnetfiddle: https://dotnetfiddle.net/z3xuqX中的工作示例

打字员是对的,你需要发布你的JSON以获得更好的答案,同时看看下面是否有帮助,

大多数浏览器都支持JSON.parse(),它在ECMA-262第5版(JS基于的规范)中定义。它的用法很简单:

var json = '{"result":true,"count":1}',obj = json .parse(json);

警报(obj.count);

现在忽略这个答案,因为OP已经发布了实际的JSON。我保存着这个,希望它仍然能帮助别人。

如果你的Json字符串是这样的-

[
      "competitor":{
         "description":"anything that you want to write",
         "Team":"Team1",        
         "outcomeDateTime":"2014-06-16T05:01:10-04:00",      
      },
      "competitor":{
         "description":"anything that you want to write",
         "team":"Team2",        
         "outcomeDateTime":"2014-06-16T05:01:10-04:00",      
      }   
]

创建一个像-

这样的模型类
public class Competitor
{
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("team")]
    public string Team { get; set; }
    [JsonProperty("outcomeDateTime")]
    public string OutcomeDateTime { get; set; }
}

你可以像-

那样反序列化
var competitors = JsonConvert.DeserializeObject<List<Competitor>>(jsonString);

你需要有一个类,其中包含json字符串包含的所有属性,例如你有一个名为jsontext的类。

使用Json-Net来反序列化响应。要生成类,你可以使用这个生成器,它接受一个Json输入并输出相关的c#类。

//....
        var responseObject = JsonConvert.DeserializeObject<RootObject>(jsontext);
    }

这会将json字符串转换为对象值

相关内容

  • 没有找到相关文章

最新更新