Parsing JSON feed using JSON.Net

时光总嘲笑我的痴心妄想 提交于 2019-12-16 18:06:33

问题


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

回答1:


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



回答2:


This code will show all the necessary fields:

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)));

    }
}

Working example in dotnetfiddle: https://dotnetfiddle.net/z3xuqX




回答3:


Typist is right, you need to post your JSON to get better answers, in the mean time see if below helps,

Most browsers support JSON.parse(), which is defined in ECMA-262 5th Edition (the specification that JS is based on). Its usage is simple:

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

alert(obj.count);




回答4:


Ignore this answer now as OP has posted the actual JSON. I'm keeping this in hope that this will still help somebody else.

If your Json string is like this -

[
      "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",      
      }   
]

Create a model class like -

public class Competitor
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("team")]
    public string Team { get; set; }

    [JsonProperty("outcomeDateTime")]
    public string OutcomeDateTime { get; set; }
}

You can deserialize like -

var competitors = JsonConvert.DeserializeObject<List<Competitor>>(jsonString);



回答5:


You need to have a class with all the properties which your json string contains , say for example you have that class called jsontext.

Use Json-Net to deserialize the response. To generate classes, you can use this generator which takes a Json input and outputs the C# classes associated.

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

this will convert your json string to that object values



来源:https://stackoverflow.com/questions/24254964/parsing-json-feed-using-json-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!