Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 无法使用c获取JSON字符串中的特定值#_C#_Json - Fatal编程技术网

C# 无法使用c获取JSON字符串中的特定值#

C# 无法使用c获取JSON字符串中的特定值#,c#,json,C#,Json,我的JSON字符串如下 { "ver": 1, "start": "2012-11-01T00:00:00-07:00", "end": "2013-11-16T23:59:59-08:00", "series": [ { "ts": "2012-11-01T00:00:00-07:00", "prod": 291.57, "cons": 524.60, "data": [ 268.47, 477.58, 5279.38 ] }, { "ts":

我的JSON字符串如下

{
"ver": 1,
"start": "2012-11-01T00:00:00-07:00",
"end": "2013-11-16T23:59:59-08:00",
"series": [
{
  "ts": "2012-11-01T00:00:00-07:00",
  "prod": 291.57,
  "cons": 524.60,
  "data": [
    268.47,
    477.58,
    5279.38
  ]
},
{
  "ts": "2012-12-01T00:00:00-08:00",
  "prod": 222.37,
  "cons": 541.24,
  "data": [
    222.37,
    531.36,
    5513.27
  ]
},
{
  "ts": "2013-01-01T00:00:00-08:00",
  "prod": 333.72,
  "cons": 599.01,
  "data": [
    320.82,
    555.91,
    5855.51
  ]
},
{
  "ts": "2013-02-01T00:00:00-08:00",
  "prod": 374.20,
  "cons": 566.03,
  "data": [
    356.57,
    548.40,
    6229.71
  ]
},
{
  "ts": "2013-03-01T00:00:00-08:00",
  "prod": 465.67,
  "cons": 620.10,
  "data": [
    465.96,
    610.50,
    6695.38
  ]
},
{
  "ts": "2013-04-01T00:00:00-07:00",
  "prod": 552.11,
  "cons": 593.60,
  "data": [
    524.16,
    577.80,
    7248.34
  ]
},
{
  "ts": "2013-05-01T00:00:00-07:00",
  "prod": 291.44,
  "cons": 303.80,
  "data": [
    291.44,
    303.80,
    7539.78
  ]
},
{
  "ts": "2013-06-01T00:00:00-07:00",
  "prod": null,
  "cons": null,
  "data": [
    null,
    null,
    null
  ]
},
{
  "ts": "2013-07-01T00:00:00-07:00",
  "prod": null,
  "cons": null,
  "data": [
    null,
    null,
    null
  ]
},
{
  "ts": "2013-08-01T00:00:00-07:00",
  "prod": 440.62,
  "cons": 365.94,
  "data": [
    440.62,
    364.65,
    9496.21
  ]
},
{
  "ts": "2013-09-01T00:00:00-07:00",
  "prod": 446.36,
  "cons": 372.21,
  "data": [
    448.57,
    370.50,
    9951.88
  ]
},
{
  "ts": "2013-10-01T00:00:00-07:00",
  "prod": 428.71,
  "cons": 361.85,
  "data": [
    428.52,
    361.66,
    10380.41
  ]
},
{
  "ts": "2013-11-01T00:00:00-07:00",
  "prod": 172.56,
  "cons": 230.56,
  "data": [
    172.56,
    229.85,
    10584.37
  ]
}
],
"DateOnly": true
}
我创建了以下类:

public class SystemComponentFinalResponseWrapper
{
    public List<SystemComponentFinalseries> series { get; set; }
}

public class SystemComponentFinalseries
{
    public string ts { get; set; }
    public double prod { get; set; }
    public double cons { get; set; }
    public double[] data {get; set; }
    //public IDictionary<string, string> data { get; set; }

}
公共类SystemComponentFinalResponseWrapper
{
公共列表系列{get;set;}
}
公共类SystemComponentFinalseries
{
公共字符串ts{get;set;}
公共双prod{get;set;}
公共双cons{get;set;}
公共双[]数据{get;set;}
//公共IDictionary数据{get;set;}
}
我正在我的代码中实现它,如下所示:

var json2 = (JObject)JsonConvert.DeserializeObject(resultJSON2);

            var d2 = JsonConvert.DeserializeObject<SystemComponentFinalResponseWrapper>(json2 .ToString());
            foreach (var series in d2.series)
            {
                Label4.Text = series.prod.ToString();
            }
var json2=(JObject)JsonConvert.DeserializeObject(resultJSON2);
var d2=JsonConvert.DeserializeObject(json2.ToString());
foreach(d2.系列中的var系列)
{
Label4.Text=series.prod.ToString();
}
我想在json中获取“series”中的所有三个“data”值,但在少数情况下,数组数据为null,因此我得到了一个错误。我尝试打印
prod
,但也出现了错误

如果数据键中的数据为null,如何获取数据键的值以及如何处理异常


我也试过
IDictionary
,它也给出了错误。

试试这个类。它也适用于可为空的值

public class SystemComponentFinalResponseWrapper
{
    public List<SystemComponentFinalseries> series { get; set; }
}

public class SystemComponentFinalseries
{
    public string ts { get; set; }
    public double? prod { get; set; }
    public double? cons { get; set; }
    public List<double?> data { get; set; }
    //public IDictionary<string, string> data { get; set; }

}
公共类SystemComponentFinalResponseWrapper
{
公共列表系列{get;set;}
}
公共类SystemComponentFinalseries
{
公共字符串ts{get;set;}
公共双?prod{get;set;}
公共双反{get;set;}
公共列表数据{get;set;}
//公共IDictionary数据{get;set;}
}

如果仍然出现错误,请告诉我。

您是否尝试过此公共列表数据{get;set;}还没有,以及如何确定何时使用列表和何时使用数组?太好了!!成功了。谢谢