Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#JSON解析问题_C#_Asp.net_Json_Parsing - Fatal编程技术网

C#JSON解析问题

C#JSON解析问题,c#,asp.net,json,parsing,C#,Asp.net,Json,Parsing,嗨,我现在正在做我最后一年的项目,快结束了。然而,我被一件小事缠住了,那就是在C#中解析下面的JSON字符串 我的坏。。。复制并粘贴了JSON文件的摘录。这是我检查jsonutils.org时有效的整个JSON字符串 { "activities-steps" : [ { "dateTime" : "2016-02-10", "value" : "2624" } ], "activiti

嗨,我现在正在做我最后一年的项目,快结束了。然而,我被一件小事缠住了,那就是在C#中解析下面的JSON字符串

我的坏。。。复制并粘贴了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值,然后是第二个对象中的time和value项的值。我已经实现了下面的代码,但是它一直给我一个
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; }
}

非常感谢所有帮助:)

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

class RootObject{
public List<Activity> activitiessteps
public List<Dataset> activitiesstepsintraday
//and so on..
}
类根对象{
公开列表活动步骤
公开列表活动步骤进入目录
//等等。。
}
数据集应为:

class Dataset {
public List<Activity> dataset
}
类数据集{
公共列表数据集
}

如果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; }
}
公共类根对象
{
[JsonProperty(“活动步骤”)]
公共列表活动步骤{get;set;}
[JsonProperty(“日内活动步骤”)]
公共活动Granular Activities Steps intraday{get;set;}
}
公共课堂活动
{
[JsonProperty(“日期时间”)]
公共字符串日期时间{get;set;}
[JsonProperty(“价值”)]
公共字符串值{get;set;}
}
公共课堂活动
{
[JsonProperty(“数据集”)]
公共列表数据集{get;set;}
[JsonProperty(“DataSetterval”)]
public int DatasetInterval{get;set;}
[JsonProperty(“数据集类型”)]
公共字符串数据集类型{get;set;}
}
公共类活动GranularDataPoint
{
[JsonProperty(“时间”)]
公共字符串时间{get;set;}
[JsonProperty(“价值”)]
公共int值{get;set;}
}

对象定义和JSON不匹配,方法不正确

// 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库,为了通过匹配签名来解决问题,可以使用属性来重命名类属性,而无需使用该方法

因此,以下修改将基于您提供的JSON解决空问题

[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; }
}
[DataContract]
公共类根对象
{
[DataMember(Name=“活动步骤”)]
公共活动[]活动_步骤{get;set;}
[DataMember(Name=“活动步骤日内”)]
公共活动粒状活动\u粒状{get;set;}
}
公共课堂活动
{
公共字符串日期时间{get;set;}
公共字符串值{get;set;}
}
[数据合同]
公共课堂活动
{
公共列表数据集{get;set;}
[DataMember(Name=“datasetterval”)]
公共字符串数据集粒度{get;set;}
[DataMember(Name=“datasetype”)]
公共字符串datasetGranularityType{get;set;}
}
公共类活动GranularDataPoint
{
公共字符串时间{get;set;}
公共int值{get;set;}
}

您是如何发布数据的?我们可以在WebAPI或控制器中看到AJAX调用和端点吗?别担心,我误解了您的做法。我正在对Fitbit API使用HttpRequest,并将响应存储在WebResponse变量中。然后我使用流读取器将响应字符串读入resultsJSON字符串我想读取第一个对象中的dateTime值,然后遍历第二个对象中的每个time和value项在哪一行出现NulReferenceException?
[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; }
}