【问题标题】:Deserialize c# class structure containing RootObject反序列化包含RootObject的c#类结构
【发布时间】: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


【解决方案1】:

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

public class RootObject
{
    public Status status { get; set; }
    public Payload payload { get; set; }
}

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 List<VehicleSummary> vehicleSummary { get; set; }
}

public class VehicleSummary
{
    public string vin { get; set; }
    public string vehicleIdentifier { get; set; }
    public string modelName { get; set; }
    public string modelYear { get; set; }
    public string nickName { get; set; }
    public int generation { get; set; }
    public string extColorCode { get; set; }
    public string trim { get; set; }
    public Image imagePath { get; set; }
    public int enrollmentStatus { get; set; }
    public int fatcAvailable { get; set; }
    public int telematicsUnit { get; set; }
    public int fuelType { get; set; }
    public string colorName { get; set; }
    public int activationType { get; set; }
    public string mileage { get; set; }
    public string dealerCode { get; set; }
    public List<MobileStore> mobileStore { get; set; }
    public SupportedApp supportedApp { get; set; }
    public int supportAdditionalDriver { get; set; }
    public int customerType { get; set; }
    public string vehicleKey { get; set; }
}

public class Image
{
    public string imageName { get; set; }
    public string imagePath { get; set; }
    public string imageType { get; set; }
    public ImageSize imageSize { get; set; }
}

public class ImageSize
{
    public string length { get; set; }
    public string width { get; set; }
    public int uom { get; set; }
}

public class MobileStore
{
    public int osType { get; set; }
    public string downloadURL { get; set; }
    public Image image { get; set; }
}

public class SupportedApp
{
    public string appType { get; set; }
    public Image appImage { get; set; }
}

我可以像这样使用JavaScriptSerializer 很好地反序列化 JSON:

var root = new JavaScriptSerializer().Deserialize<RootObject>(result);

result 是您在问题中发布的 JSON 字符串。

但是,请注意,如果您将类放在另一个名为 TestStruct 的类中,那么您需要考虑到这一点并反序列化为 TestStruct.RootObject,例如:

var root = new JavaScriptSerializer().Deserialize<TestStruct.RootObject>(result);

我还能够使用 Json.Net 以与 JsonConvert 类相同的方式反序列化 JSON:

var root = JsonConvert.DeserializeObject<RootObject>(result);

一旦你有了反序列化的对象,你就可以像这样从中提取一些有趣的信息:

foreach (var vs in root.payload.vehicleSummary)
{
    Console.WriteLine(string.Format("{0} - {1} {2} {3}, {4} mi", 
        vs.vin, vs.colorName, vs.modelYear, vs.modelName, vs.mileage));
}

这是一个使用 Json.Net 的工作演示:https://dotnetfiddle.net/Zh35be

【讨论】:

  • 谢谢布赖恩。这就是我认为的解决方案。但是在我的 ASP.NET 4.5 项目中,我无法让 RootObject 显示为有效! Intellisense 没有显示出来。我有“使用 Newtonsoft.Json;”在页面中。但是,在我剪切并粘贴了您提供的示例之后,RootObject 现在以智能感知方式显示并且有效。看来这是 Visual Studio 中的一个错误!!!!
猜你喜欢
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2017-09-13
相关资源
最近更新 更多