如何在C#

时间:2017-02-07 13:23:31

标签: c# json.net

我想在C#中使用Newtonsoft JsonConvert将javascript数组序列化为对象。任何帮助将不胜感激。

我的Javascript数组对象是:

[
  {
    "ID": 0,
    "Patient_ID": 1,
    "Appointment_ID": 219,
    "ObservationID": "33",
    "arrOption": "{\"ObservationOptionList\":[{\"Observations_Param_Option_ID\":\"77\",\"Extended_Value\":\"\"}]}"
  },
  {
    "ID": 0,
    "Patient_ID": 1,
    "Appointment_ID": 219,
    "ObservationID": "1",
    "arrOption": "{\"ObservationOptionList\":[{\"Observations_Param_Option_ID\":\"1\",\"Extended_Value\":\"Dry\"}]}"
  },
  {
    "ID": 0,
    "Patient_ID": 1,
    "Appointment_ID": 219,
    "ObservationID": "8",
    "arrOption": "{\"ObservationOptionList\":[{\"Observations_Param_Option_ID\":\"25\",\"Extended_Value\":\"\"}]}"
  },
  {
    "ID": 0,
    "Patient_ID": 1,
    "Appointment_ID": 219,
    "ObservationID": "15",
    "arrOption": "{\"ObservationOptionList\":[{\"Observations_Param_Option_ID\":\"40\",\"Extended_Value\":\"\"}]}"
  }
]

2 个答案:

答案 0 :(得分:1)

我认为你的意思是 de -serialize。

创建一个班级来容纳每位患者。

public class Patient
{
    public int ID { get; set; }
    public int Patient_ID { get; set; }
    public int Appointment_ID { get; set; }
    public string ObservationID { get; set; }
    public string arrOption { get; set; }
}

反序列化为患者名单。

var patients = JsonConvert.DeserializeObject<List<Patient>>(json);

答案 1 :(得分:-1)

创建C#类

class MyClass
{
    public int ID {get;set;}
    public int Patient_ID {get;set;}
    public int Appointment_ID {get;set;}
    public string ObservationID {get;set;}
    public string arrOption {get;set;

}

现在使用反序列化方法

new JavaScriptSerializer().Deserialize<MyClass>(jsonString);
  

如果您还希望将arrOption反序列化为反序列化后需要预告的对象