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

时间:2013-11-20 10:36:04

标签: 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": "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; }

}

我正在我的代码中实现它:

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

            var d2 = JsonConvert.DeserializeObject<SystemComponentFinalResponseWrapper>(json2 .ToString());
            foreach (var series in d2.series)
            {
                Label4.Text = series.prod.ToString();
            }

我想在json中的“series”中获取“data”的所有三个值,但是在少数数组数据中为null,所以我收到错误。我尝试打印prod,但它也给出错误。

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

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

1 个答案:

答案 0 :(得分:0)

试试这堂课。它也适用于可以为空的值。

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

}

如果您仍然收到错误,请告诉我。