我想在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":""}]}"
}
]
我假设你的意思是反序列化。
创建一个类来容纳每个患者。
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);
创建 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
对象,则需要在反序列化后对对象进行反序列化