【发布时间】:2020-03-24 19:13:12
【问题描述】:
我有一个 JSON API 结果,我通过在线 JSON-to-C# 结构程序处理该结果以创建类结构。我在其他项目中多次使用它。 JSON 返回所有内容以及一个公共类RootObject,该类引用返回值的状态和有效负载段。
我正在使用 ASP.NET C# 库使用 JavaScriptSerializer 反序列化结果 JSON:
var vlist = new JavaScriptSerializer().Deserialize<TestStruct>(result);
我的数据结构是这样的(很标准):
public class TestStruct
{
public class Status
{
public int statusCode { get; set; }
public int errorType { get; set; }
public int errorCode { get; set; }
public string errorMessage { get; set; }
}
public class Payload
{
public VehicleStatusRpt vehicleStatusRpt { get; set; }
}
public class VehicleStatusRpt
{
public string statusType { get; set; }
//public ReportDate reportDate { get; set; }
//public VehicleStatus vehicleStatus { get; set; }
}
public class RootObject
{
public Status status { get; set; }
public Payload payload { get; set; }
}
}
我尝试使用类结构解析的完整 JSON 结果是:
{
"status": {
"statusCode": 0,
"errorType": 0,
"errorCode": 0,
"errorMessage": "Success with response body"
},
"payload": {
"vehicleSummary": [
{
"vin": "KNDJX3AE8E7000080",
"vehicleIdentifier": "000080",
"modelName": "SOUL EV",
"modelYear": "2015",
"nickName": "My SOUL",
"generation": 1,
"extColorCode": "1D",
"trim": "EV",
"imagePath": {
"imageName": "2015-soul_ev-ev-1d.png",
"imagePath": "/content/dam/kia/us/owners/image/vehicle/2015/soul_ev/ev/",
"imageType": "1",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
},
"enrollmentStatus": 1,
"fatcAvailable": 1,
"telematicsUnit": 1,
"fuelType": 4,
"colorName": "CLEAR WHITE",
"activationType": 1,
"mileage": "24410",
"dealerCode": "MOBISDLR1",
"mobileStore": [
{
"osType": 0,
"downloadURL": "https://itunes.apple.com/us/app/kia-access-with-uvo-link/id1280548773?mt=8",
"image": {
"imageName": "iosImage.png",
"imagePath": "/content/dam/kia/us/owners/image/common/app/",
"imageType": "2",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
}
},
{
"osType": 1,
"downloadURL": "https://play.google.com/store/apps/details?id=com.myuvo.link",
"image": {
"imageName": "androidImage.png",
"imagePath": "/content/dam/kia/us/owners/image/common/app/",
"imageType": "2",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
}
}
],
"supportedApp": {
"appType": "5",
"appImage": {
"imageName": "app-access.png",
"imagePath": "/content/dam/kia/us/owners/image/common/app/access/",
"imageType": "2",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
}
},
"supportAdditionalDriver": 0,
"customerType": 0,
"vehicleKey": "937db044-8328-4188-a3d2-68ac3b183752"
}
]
}
}
我通过 json2csharp.com 运行这个来获取结构(上面的示例只是一个缩写的“测试”
反序列化器返回错误:Invalid JSON Primitive (starting with Payload)
我看到了使用 RootObject 但使用 Newtonsoft JSON 库的示例。我想使用 Microsoft 库。我真的需要切换到 Newtonsoft JSON 吗?如果我可以使用JavaScriptSerializer 库,如何使用?
【问题讨论】:
-
您能向我们展示您尝试反序列化到这些类中的 JSON 吗?
-
完整响应是:哎呀。太长,无法粘贴到这里。
-
我现在正在使用 Newtonsoft json。仍然无法正常工作。
-
现在使用 JsonConvert。我看到使用 RootObject 的示例,但我没有这个库。 Net.Framework 4.5 Visual Studio 2017
标签: c# json json.net javascriptserializer