【问题标题】:C# JSON Parsing IssueC# JSON 解析问题
【发布时间】:2016-02-10 19:34:06
【问题描述】:

您好,我目前正在做我最后一年的项目,并且快要结束了。但是,我被困在一件小事上,那就是用 C# 解析下面的 JSON 字符串。

My Bad... 复制并粘贴了 JSON 文件的摘录。这是我检查 jsonutils.org 时有效的整个 JSON 字符串

{ 
    "activities-steps" : [ 
        { 
            "dateTime" : "2016-02-10",
            "value" : "2624"
        } 
    ],
    "activities-steps-intraday" : { 
        "dataset" : [ 
        { 
            "time" : "00:00:00",
            "value" : 0
        },
        { 
            "time" : "00:01:00",
            "value" : 0
        },
        { 
            "time" : "00:02:00",
            "value" : 0
        },
        { 
            "time" : "00:03:00",
            "value" : 0
        },
        { 
            "time" : "00:04:00",
            "value" : 0
        },
        { 
            "time" : "00:05:00",
            "value" : 0
        },
        { 
            "time" : "00:06:00",
            "value" : 0
        },
        { 
            "time" : "00:07:00",
            "value" : 0
        },
        { 
            "time" : "00:08:00",
            "value" : 0
        },
        { 
            "time" : "00:09:00",
            "value" : 0
        },
        { 
            "time" : "00:10:00",
            "value" : 0
        },
        { 
            "time" : "00:11:00",
            "value" : 0
        },
        { 
            "time" : "00:12:00",
            "value" : 0
        },
        { 
            "time" : "00:13:00",
            "value" : 0
        },
        { 
            "time" : "00:14:00",
            "value" : 0
        },
        { 
            "time" : "00:15:00",
            "value" : 0
        },
        { 
            "time" : "00:16:00",
            "value" : 0
        },
        { 
            "time" : "00:17:00",
            "value" : 0
        }
        ],
        "datasetInterval" : 1,
        "datasetType" : "minute"
    }
}

我需要第一个 JSON 对象中的 dateTime 值,然后是第二个对象中的时间和值项的值。我已经实现了下面的代码,但是它一直给我一个NullReferenceException

这些方法用于帮助解析 JSON 对象

public class RootObject
{
    public Activity activity { get; set; }
    public ActivityGranular activity_granular { get; set; }
}

public class Activity
{
    public string dateTime { get; set; }
    public string value { get; set; }
}

 public class ActivityGranular
{
    public List<ActivityGranularDatapoint> dataset { get; set; }
    public int datasetGranularity { get; set; }
    public string datasetGranularityType { get; set; }
}

public class ActivityGranularDatapoint
{
    public string time { get; set; }
    public int value { get; set; }
}

Main 类中的 MainCode 在执行时在上面的根对象类的两个根元素中返回 null 并给出 NullReferenceExceptions

resultsJSON.Replace("-", "_");

root = JsonConvert.DeserializeObject<RootObject>(resultsJSON);

activity = root.activites_steps;
activityGranular = root.activities_steps_intraday;

for (int a = 0; a < activityGranular.dataset.Count; a++)
{
    activityGranularDatapoint[a].time = activityGranular.dataset[a].time;
   activityGranularDatapoint[a].value = activityGranular.dataset[a].value;
}

非常感谢所有帮助:)

【问题讨论】:

  • 您如何发布数据?我们可以在 WebAPI 或您的控制器中看到 AJAX 调用和端点吗?
  • 没关系 - 误解了你在做什么。
  • 我在 Fitbit API 中使用 HttpRequest 并将响应存储在 WebResponse 变量中。然后我使用 Stream reader 将响应字符串读入 resultsJSON 字符串
  • 我想读取第一个对象中的 dateTime 值,然后遍历第二个对象中的每个时间和值项
  • 您在哪一行出现 NulReferenceException?

标签: c# asp.net json parsing


【解决方案1】:

如果您的 json 中的属性名称不符合您的需求,请使用 JsonProperty 属性。只是不要使用:

  • 解决方法或破解 json 数据。
  • 不要替换json的任何内容
  • 顺其自然。

下面的代码只是通过重命名你的类的属性来工作

public class RootObject
{
    [JsonProperty("activities-steps")]
    public List<Activity> ActivitesSteps { get; set; }

    [JsonProperty("activities-steps-intraday")]
    public ActivityGranular ActivitiesStepsIntraday { get; set; }
}

public class Activity
{
    [JsonProperty("dateTime")]
    public string DateTime { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
}

public class ActivityGranular
{
    [JsonProperty("dataset")]
    public List<ActivityGranularDatapoint> DataSet { get; set; }

    [JsonProperty("datasetInterval")]
    public int DatasetInterval { get; set; }

    [JsonProperty("datasetType")]
    public string DatasetType { get; set; }
}

public class ActivityGranularDatapoint
{
    [JsonProperty("time")]
    public string Time { get; set; }

    [JsonProperty("value")]
    public int Value { get; set; }
}

【讨论】:

    【解决方案2】:

    请记住,json 中的字段需要与类的字段完全匹配。 例如,RootObject 应该是这样的:

    class RootObject{
    public List<Activity> activitiessteps
    public List<Dataset> activitiesstepsintraday
    //and so on..
    }
    

    数据集应该是:

    class Dataset {
    public List<Activity> dataset
    }
    

    【讨论】:

      【解决方案3】:

      对象定义与 JSON 不匹配,Replace 方法不正确。

      // Replace does not replace the values in the original variable, 
      // it returns a string representing the original with the replaced values,
      // so you must reassign it to a variable as such.
      
      resultsJSON = resultsJSON.Replace("-", "_");
      

      假设您使用的是 Newtonsoft.Json 库,为了解决匹配签名的问题,可以使用 DataContract 属性重命名您的类属性,而无需使用 Replace 方法。

      因此,以下修改将根据您提供的 JSON 解决 null 问题。

      [DataContract]
      public class RootObject
      {
          [DataMember(Name = "activities-steps")]
          public Activity[] activity_steps { get; set; }
          [DataMember(Name = "activities-steps-intraday")]
          public ActivityGranular activity_granular { get; set; }
      
      }
      
      public class Activity
      {
          public string dateTime { get; set; }
          public string value { get; set; }
      }
      
      [DataContract]
      public class ActivityGranular
      {
          public List<ActivityGranularDatapoint> dataset { get; set; }
          [DataMember(Name = "datasetInterval")]
          public string datasetGranularity { get; set; }
          [DataMember(Name = "datasetType")]
          public string datasetGranularityType { get; set; }
      }
      
      public class ActivityGranularDatapoint
      {
          public string time { get; set; }
          public int value { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-09
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 2019-01-30
        相关资源
        最近更新 更多