3

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

6
  • 1
    you forget to add attributes to properties? like [JsonConverter(typeof(Int32))] Commented Apr 2, 2014 at 10:29
  • 2
    it is not a list, only one PatientData object Commented Apr 2, 2014 at 10:32
  • esskar you are right!! by changing LPatientData = new List<ObjPatientData.RootObject>(); LPatientData = JsonConvert.DeserializeObject<List<ObjPatientData.RootObject>>(Json); to LPatientData = new List<ObjPatientData.PatientData>(); LPatientData = JsonConvert.DeserializeObject<List<ObjPatientData.PatientData>>(Json); it works !! Commented Apr 2, 2014 at 10:33
  • Now please can someone instruct me on how to accept esskar answer and close this question? Thanks :-) Commented Apr 2, 2014 at 10:35
  • you can answer your own question and accept it ;) Commented Apr 2, 2014 at 10:36

2 Answers 2

2

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>>();
Sign up to request clarification or add additional context in comments.

Comments

0

I think following the code will work.

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

2 Comments

I tried that before but I get an error of type of .. is not supported for deserialization of an array
@brillox I have tested this code and it is working fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.