Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化包含RootObject的c类结构_C#_Json_Json.net_Javascriptserializer - Fatal编程技术网

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

C# 反序列化包含RootObject的c类结构,c#,json,json.net,javascriptserializer,C#,Json,Json.net,Javascriptserializer,我有一个JSON API结果,我通过一个在线JSON-to-C结构程序来处理它,以创建类结构。我已经在其他项目中使用过很多次了。JSON返回所有内容以及一个公共类RootObject,该对象引用返回值的状态和有效负载段 我正在使用ASP.NET C库使用JavaScriptSerializer反序列化结果JSON: 我尝试使用类结构解析的完整JSON结果是: { "status": { "statusCode": 0, "errorType": 0, "errorCo

我有一个JSON API结果,我通过一个在线JSON-to-C结构程序来处理它,以创建类结构。我已经在其他项目中使用过很多次了。JSON返回所有内容以及一个公共类RootObject,该对象引用返回值的状态和有效负载段

我正在使用ASP.NET C库使用JavaScriptSerializer反序列化结果JSON:

我尝试使用类结构解析的完整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运行这个程序来获得结构。上面的示例只是一个缩写的“测试”

反序列化程序返回错误:以有效负载开头的JSON基元无效


我看到了使用RootObject但使用Newtonsoft JSON库的示例。我想使用微软图书馆。我真的需要切换到Newtonsoft JSON吗?如果我可以使用JavaScriptSerializer库,如何使用?

与您发布的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; }
}
其中result是您在问题中发布的JSON字符串

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

var root = new JavaScriptSerializer().Deserialize<TestStruct.RootObject>(result);
获得反序列化对象后,可以从中提取一些有趣的信息,如下所示:

var root = new JavaScriptSerializer().Deserialize<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的工作演示:

您可以向我们展示您试图反序列化到这些类中的Json吗?完整的响应是:Oops。太长,无法粘贴到这里。我现在使用的是Newtonsoft json。仍然无法正常工作。正在使用JsonConvert。我看到了使用RootObject的示例,但我没有这个库。Net.Framework 4.5 Visual Studio 2017谢谢Brian。这就是我所认为的解决办法。然而,在我的ASP.NET 4.5项目中,我无法让RootObject显示为有效!智能感知没有显示出来。我已经使用了Newtonsoft.Json;在页面中。然而,在我剪切并粘贴了您提供的示例之后,RootObject现在以intellisense显示,并且是有效的。这似乎是Visual Studio中的一个bug!!!!
var root = new JavaScriptSerializer().Deserialize<TestStruct.RootObject>(result);
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));
}