反序列化包含RootObject的c#类结构

问题描述 投票:0回答:1

我有一个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来运行以获取结构(上面的示例仅是缩写的“ test”)>

反序列化器返回错误:无效的JSON基元(从有效负载开始)

[我看到了使用RootObject的示例,但使用了Newtonsoft JSON库。我想使用Microsoft库。我真的需要切换到Newtonsoft JSON吗?如果可以使用JavaScriptSerializer库,怎么办?

我有一个JSON API结果,我通过在线JSON-to-C#结构程序进行了处理以创建类结构。我已经在其他项目中使用了很多次。 JSON沿...返回了所有内容...

c# json json.net javascriptserializer
1个回答
1
投票

与您发布的JSON相对应的类是:

© www.soinside.com 2019 - 2024. All rights reserved.