找不到JSON解析器元素

时间:2014-06-17 22:05:35

标签: c# json json.net linq-to-objects

这是我的JSON

{
   "State of Origin 2014":{
      "1471137":{
         "EventID":1471137,
         "ParentEventID":1471074,
         "MainEvent":"State Of Origin Series 2014",
         "Competitors":{
            "ActiveCompetitors":3,
            "Competitors":[
               {
                  "Team":"New South Wales (2 - 1)",
                  "Win":"2.15",

               },
               {
                  "Team":"New South Wales (3 - 0)",
                  "Win":"3.05",

               },
               {
                  "Team":"Queensland (2 - 1)",
                  "Win":"3.30",

               }
            ],
            "TotalCompetitors":3,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      },
      "3269132":{
         "EventID":3269132,
         "ParentEventID":0,
         "MainEvent":"New South Wales v Queensland",
         "Competitors":{
            "Margin1Low":1,
            "Competitors":[
               {
                  "Name":"New South Wales",
                  "Win":"1.60",

               },
               {
                  "Name":"Queensland",
                  "Win":"2.35",

               }
            ],
            "HasWinOdds":true,
            "TotalOddsColumns":2,
            "MarketCount":1,
            "PerformVideo":false
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      }
   }
}

我正在使用JSON.Net并且一切正常但在我的一些数据中缺少一些元素字段,例如我在竞争对手中获得了Team元素

 Teams = from JObject comps in value["Competitors"]["Competitors"]
                       select (string)comps["Team"]

但是在某些数据中,Team元素缺失,我想抓取Name Element,所以我得到的Object引用没有设置为对象的实例。错误。

这是我的代码

    var query =
       from JProperty ev in obj.AsJEnumerable()
       from JProperty evid in ev.Value.AsJEnumerable()
       let value = (JObject)evid.Value
       select new Person
       {
           EventID = (string)value["EventID"],
           Description = (string)value["MainEvent"],
           OutcomeDateTime = (string)value["OutcomeDateTime"],
           EventStatus = (string)value["EventStatus"],
           Teams = from JObject comps in value["Competitors"]["Competitors"]
               select (string)comps["Team"]

       };

    foreach (var b in query)
    {

        string description = b.Description;
        string OutcomeDateTime = b.OutcomeDateTime;
        IEnumerable<string> _team = b.Teams;
        foreach (var teams in _team)
        {

            string team = teams.ToString();


        }
        Console.WriteLine(description);
        Console.WriteLine(OutcomeDateTime);


    }

如果Team元素不存在,我如何获得Name元素值?

2 个答案:

答案 0 :(得分:0)

您可以将json反序列化为具体类

var obj = JsonConvert.DeserializeObject < Dictionary<string, Dictionary<string,RootObject>>>(json);

public class Competitor
{
    public string Team { get; set; }
    public string Win { get; set; }
}

public class CompetitorsClass
{
    public int ActiveCompetitors { get; set; }
    public List<Competitor> Competitors { get; set; }
    public int TotalCompetitors { get; set; }
    public bool HasWinOdds { get; set; }
}

public class RootObject
{
    public int EventID { get; set; }
    public int ParentEventID { get; set; }
    public string MainEvent { get; set; }
    public CompetitorsClass Competitors { get; set; }
    public string EventStatus { get; set; }
    public bool IsSuspended { get; set; }
    public bool AllowBets { get; set; }
}

BTW:什么是OutcomeDateTime?你的json里没有这样的领域。

答案 1 :(得分:0)

以下内容有效。如果“Team”为null,则使用null-coalescing运算符来获取“Name”。 http://msdn.microsoft.com/en-us/library/ms173224.aspx

Teams = from JObject comps in value["Competitors"]["Competitors"]
                        select (string)comps["Team"] ?? (string)comps["Name"]