public static T FromJsonTo<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T jsonObject = (T)ser.ReadObject(ms);
return jsonObject;
}
[DataContract] //非常重要,没有这语句,则无法进行反序列化Json对象
public class pmGPS
{
[DataMember]//非常重要
public decimal speed { get; set; }
[DataMember]
public decimal altitude { get; set; }
[DataMember]
public decimal latitude { get; set; }
[DataMember]
public decimal longitude { get; set; }
[DataMember]
public equipmentType equipment { get; set; }
[DataMember]
public gpstimecar gpstime { get; set; }
//构造函数
public pmGPS(decimal pmId, decimal pmLat, decimal pmlatitude, decimal pmlongitude, equipmentType pmeq, gpstimecar pmgpstcar)
{
speed = pmId;
altitude = pmLat;
latitude = pmlatitude;
longitude = pmlongitude;
equipment = pmeq;
gpstime = pmgpstcar;
}
}
[DataContract] //非常重要,没有这语句,则无法进行反序列化Json对象
public class equipmentType
{
[DataMember]//非常重要
public string gpsid { get; set; }
[DataMember]
public string id { get; set; }
[DataMember]
public int isonline { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public department department { get; set; }
//构造函数
public equipmentType(string pmgpsid, string pmid, int pmisonline, string pmname, department pmdepartment)
{
gpsid = pmgpsid;
id = pmid;
isonline = pmisonline;
name = pmname;
department = pmdepartment;
}
}
[DataContract] //非常重要,没有这语句,则无法进行反序列化Json对象
public class department
{
[DataMember]//非常重要
public string department_name { get; set; }
//构造函数
public department(string pmdepartment_name)
{
department_name = pmdepartment_name;
}
}
[DataContract] //非常重要,没有这语句,则无法进行反序列化Json对象
public class gpstimecar
{
[DataMember]//非常重要
public string time { get; set; }
//构造函数
public gpstimecar(string pmtime)
{
time = pmtime;
}
}
#endregion
使用时: List<pmGPS> pmObj = FromJsonTo<List<pmGPS>>(GPScarInfo);
foreach (pmGPS gps in pmObj)
{
}