Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.Net deserialize object give me empty object

Tags:

c#

json.net

I am trying to de-serialize this json string:

{"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": []
      }
    ]
  }
]}

by using classes generated from json2csharp like this

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

    }

and calling it like this:

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

but I get a list of empty objects; any help will be appreciated.

Thanks! please note that

like image 946
brillox Avatar asked Apr 27 '26 02:04

brillox


1 Answers

Based on the above classes, I solved the issue by returning a list of "PatientData" objects rather than a list of "RootObjects" and using a Jarray.Parse(Json) in this way:

ObjPatientData.RootObject Root = new ObjPatientData.RootObject();
var jArray = JArray.Parse(Json);
Root.PatientData = jArray.ToObject<List<ObjPatientData.PatientData>>();
like image 146
brillox Avatar answered Apr 29 '26 16:04

brillox