Json.Net 反序列化对象返回空对象

3

我正在尝试反序列化这个JSON字符串:

{"PatientData":[
  {
    "device": {
      "serial": "M01106798",
      "manufacturer": "Nipro",
      "model": "TrueResult",
      "first_value_at": "2010-07-17T22:39:00",
      "last_value_at": "2010-09-30T11:18:00",
      "supports_glucose": "no",
      "supports_cgm": "no",
      "supports_insulin": "no",
      "supports_carb": "no"
    },
    "data": []
  },
  {
    "device": {
      "serial": "59-50889-10",
      "manufacturer": "Animas",
      "model": "IR1200",
      "first_value_at": "2014-02-07T11:46:00",
      "last_value_at": "2014-03-27T10:38:00",
      "supports_glucose": "no",
      "supports_cgm": "no",
      "supports_insulin": "no",
      "supports_carb": "no"
    },
    "data": [      
      {
        "type": "insulin_bolus",
        "created_at": "2014-02-07T23:42:00",
        "unit": "U",
        "total_value": 0.9,
        "spike_value": 0.9,
        "flags": [
          {
            "flag": 1008,
            "description": "Bolus inactive"
          }
        ]
      },
      {
        "type": "insulin_basal",
        "created_at": "2014-02-08T00:01:00",
        "value": 0.175,
        "unit": "U/h",
        "flags": []
      },
      {
        "type": "insulin_basal",
        "created_at": "2014-02-08T05:01:00",
        "value": 0.225,
        "unit": "U/h",
        "flags": []
      },
      {
        "type": "insulin_bolus",
        "created_at": "2014-02-08T07:42:00",
        "unit": "U",
        "total_value": 2.6,
        "duration": 1800,
        "flags": []
      },
      {
        "type": "insulin_bolus",
        "created_at": "2014-02-08T09:38:00",
        "unit": "U",
        "total_value": 0.3,
        "spike_value": 0.3,
        "flags": [
          {
            "flag": 1008,
            "description": "Bolus inactive"
          }
        ]
      },    
      {
        "type": "glucose",
        "created_at": "2014-03-27T12:55:18",
        "value": 33.167,
        "unit": "mmol/l",
        "flags": []
      }
    ]
  }
]}

通过使用从json2csharp生成的类,可以像这样进行IT技术方面的操作。
public class ObjPatientData
    {
        public class Device
        {
            public string serial { get; set; }
            public string manufacturer { get; set; }
            public string model { get; set; }
            public string first_value_at { get; set; }
            public string last_value_at { get; set; }
            public string supports_glucose { get; set; }
            public string supports_cgm { get; set; }
            public string supports_insulin { get; set; }
            public string supports_carb { get; set; }
        }

        public class PatientData
        {
            public Device device { get; set; }
            public List<object> data { get; set; }
        }

        public class RootObject
        {
            public List<PatientData> PatientData { get; set; }
        }

    }

并且像这样调用:

LPatientData = new List<ObjPatientData.RootObject>();
LPatientData = JsonConvert.DeserializeObject<List<ObjPatientData.RootObject>>(Json);

但我得到了一份空对象列表;任何帮助将不胜感激。
谢谢!请注意:

1
你忘记给属性添加属性了吗?比如[JsonConverter(typeof(Int32))]。 - Mustafa Ekici
2
只有一个 PatientData 对象,这不是列表。 - esskar
Esskar,你是对的!!通过更改LPatientData = new List<ObjPatientData.RootObject>(); LPatientData = JsonConvert.DeserializeObject<List<ObjPatientData.RootObject>>(Json);为LPatientData = new List<ObjPatientData.PatientData>(); LPatientData = JsonConvert.DeserializeObject<List<ObjPatientData.PatientData>>(Json);它可以工作!! - brillox
现在请有人告诉我如何接受esskar的答案并关闭这个问题吗?谢谢 :-) - brillox
你可以回答自己的问题并接受它 ;) - Mustafa Ekici
我如何回答我自己的问题?我没有看到任何“回答”链接或类似的东西。 - brillox
2个回答

2

基于上述类,我通过返回“PatientData”对象列表而不是“RootObjects”列表并以以下方式使用Jarray.Parse(Json)解决了该问题:

ObjPatientData.RootObject Root = new ObjPatientData.RootObject();
var jArray = JArray.Parse(Json);
Root.PatientData = jArray.ToObject<List<ObjPatientData.PatientData>>();

0

我认为遵循这段代码会起作用。

string json = "{\"PatientData\":[{\"device\": {\"serial\":    \"M01106798\",\"manufacturer\": \"Nipro\",\"model\": \"TrueResult\",\"first_value_at\": \"2010-07-17T22:39:00\",\"last_value_at\": \"2010-09-30T11:18:00\",\"supports_glucose\": \"no\",\"supports_cgm\": \"no\",\"supports_insulin\": \"no\",\"supports_carb\": \"no\"},\"data\": []},{\"device\": {\"serial\": \"59-50889-10\",\"manufacturer\": \"Animas\",\"model\": \"IR1200\",\"first_value_at\": \"2014-02-07T11:46:00\",\"last_value_at\": \"2014-03-27T10:38:00\",\"supports_glucose\": \"no\",\"supports_cgm\": \"no\",\"supports_insulin\": \"no\",\"supports_carb\": \"no\"},\"data\": [      {\"type\": \"insulin_bolus\",\"created_at\": \"2014-02-07T23:42:00\",\"unit\": \"U\",\"total_value\": 0.9,\"spike_value\": 0.9,\"flags\": [{\"flag\": 1008,\"description\": \"Bolus inactive\"}]},{\"type\": \"insulin_basal\",\"created_at\": \"2014-02-08T00:01:00\",\"value\": 0.175,\"unit\": \"U/h\",\"flags\": []},{\"type\": \"insulin_basal\",\"created_at\": \"2014-02-08T05:01:00\",\"value\": 0.225,\"unit\": \"U/h\",\"flags\": []},{\"type\": \"insulin_bolus\",\"created_at\": \"2014-02-08T07:42:00\",\"unit\": \"U\",\"total_value\": 2.6,\"duration\": 1800,\"flags\": []},{\"type\": \"insulin_bolus\",\"created_at\": \"2014-02-08T09:38:00\",\"unit\": \"U\",\"total_value\": 0.3,\"spike_value\": 0.3,\"flags\": [{\"flag\": 1008,\"description\": \"Bolus inactive\"}]},    {\"type\": \"glucose\",\"created_at\": \"2014-03-27T12:55:18\",\"value\": 33.167,\"unit\": \"mmol/l\",\"flags\": []}]}]}";


        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();

        RootObject rootObject = javaScriptSerializer.Deserialize<RootObject>(json);

我之前尝试过这个,但是出现了一个错误,类型为..的数组反序列化不受支持。 - brillox
@brillox 我已经测试了这段代码,它运行良好。 - Girish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接