【问题标题】:Deserialize JSON with multi dimensional array on C# Webapi在 C# Webapi 上使用多维数组反序列化 JSON
【发布时间】:2017-10-31 23:00:17
【问题描述】:

我有一个带有多维数组的 JSON。我不知道如何在我的 c# 模型上反序列化它。我做了一种非常简单的反序列化方法,但它不起作用。我需要知道如何以更结构化的方式反序列化我的 JSON。

这是我的 json 数据

{
"Access_point_result": [

{
  "msg": {
    "ap_eth_mac": {
      "addr": "D8C7C8C0C7BE"
    },
    "ap_name": "1344-1-AL5",
    "ap_group": "1344-hq",
    "ap_model": "135",
    "depl_mode": "DEPLOYMENT_MODE_CAMPUS",
    "ap_ip_address": {
      "af": "ADDR_FAMILY_INET",
      "addr": "10.6.66.67",
      "reboots": 1,
      "rebootstraps": 2,
      "managed_by": {
        "af": "ADDR_FAMILY_INET",
        "addr": "0.0.0.0"
      },
      "managed_by_key": "2e302bee0164cc154d1d266d8567ada44d49e77af82f4b5ccb",
      "radios": {
        "radio_bssid.addr": "D8.C7.C8.46.D8.10"
      },
      "is_master": true,
      "ap_location": {
        "ap_eth_mac": "D8C7C8C0C7BE",
        "campus_id": "6F9DEC79839D458B9F148D16A46A353E",
        "building_id": "83393A922FB249C1929B95393A2AAFDA",
        "floor_id": "260BE76B0DD13E7AAF18EB3B47DD7F7B",
        "longitude": -122.008,
        "latitude": 37.4129,
        "ap_x": 22.15,
        "ap_y": 99.18
      }
    },
    "ts": 1382046667
  }
}
]
}

下面是我的 C# 模型

 public class WifiDataAruba : BaseModel
{

    public string APMACAddr { get; set; }
    public string APName { get; set; }
    public string APGroup { get; set; }
    public string APModel { get; set; }
    public string APDeplMode { get; set; }
    public string APIPAddr { get; set; }
    public int APReboots { get; set; }
    public int APRebootStraps { get; set; }
    public string APManagedBy { get; set; }
    public string APManagedByKey { get; set; }
    public string APRadios { get; set; }
    public bool APMaster { get; set; }
    public string APLocation { get; set; }
    public string APMACAddr2 { get; set; }
    public string APCampusID { get; set; }
    public string APLocationID { get; set; }
    public string APBuildingID { get; set; }
    public string APFloorID { get; set; }
    public double APLongtitude { get; set; }
    public double APLatitude { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
    public DateTime ImportTimestamp { get; set; }
}

我怎样才能打破反序列化的结构方式?

【问题讨论】:

  • 多维数组在哪里?

标签: c# arrays json asp.net-web-api deserialization


【解决方案1】:

你必须将你的模型更改为以下

public class ApEthMac
{
    public string addr { get; set; }
}

public class ManagedBy
{
    public string af { get; set; }
    public string addr { get; set; }
}

public class Radios
{
    public string __invalid_name__radio_bssid.addr { get; set; }
}

public class ApLocation
{
    public string ap_eth_mac { get; set; }
    public string campus_id { get; set; }
    public string building_id { get; set; }
    public string floor_id { get; set; }
    public double longitude { get; set; }
    public double latitude { get; set; }
    public double ap_x { get; set; }
    public double ap_y { get; set; }
}

public class ApIpAddress
{
    public string af { get; set; }
    public string addr { get; set; }
    public int reboots { get; set; }
    public int rebootstraps { get; set; }
    public ManagedBy managed_by { get; set; }
    public string managed_by_key { get; set; }
    public Radios radios { get; set; }
    public bool is_master { get; set; }
    public ApLocation ap_location { get; set; }
}

public class Msg
{
    public ApEthMac ap_eth_mac { get; set; }
    public string ap_name { get; set; }
    public string ap_group { get; set; }
    public string ap_model { get; set; }
    public string depl_mode { get; set; }
    public ApIpAddress ap_ip_address { get; set; }
    public int ts { get; set; }
}

public class AccessPointResult
{
    public Msg msg { get; set; }
}

public class RootObject
{
    public List<AccessPointResult> Access_point_result { get; set; }
}

你的 webapi 方法应该如下所示

public void Post(RootObject rootObject)

注意 如果要将 c# 模型更改为 PascalCase,则必须在 webapi 配置中添加 camelcaseconvention 你的方法在WebaApiConfig 中应该是这样的

 public static void Register(HttpConfiguration config)
 {
     // Web API configuration and services
     EnableCrossSiteRequests(config);
     // Web API routes
     config.MapHttpAttributeRoutes();
     GlobalConfiguration.Configuration.Formatters.JsonFormatter
         .SerializerSettings.ContractResolver =  
                    new CamelCasePropertyNamesContractResolver();

     config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
 }

【讨论】:

    【解决方案2】:

    型号

    public class ApEthMac
    {
        public string addr { get; set; }
    }
    
    public class ManagedBy
    {
        public string af { get; set; }
        public string addr { get; set; }
    }
    
    public class Radios
    {
        public string __invalid_name__radio_bssid.addr { get; set; }
    }
    
    public class ApLocation
    {
        public string ap_eth_mac { get; set; }
        public string campus_id { get; set; }
        public string building_id { get; set; }
        public string floor_id { get; set; }
        public double longitude { get; set; }
        public double latitude { get; set; }
        public double ap_x { get; set; }
        public double ap_y { get; set; }
    }
    
    public class ApIpAddress
    {
        public string af { get; set; }
        public string addr { get; set; }
        public int reboots { get; set; }
        public int rebootstraps { get; set; }
        public ManagedBy managed_by { get; set; }
        public string managed_by_key { get; set; }
        public Radios radios { get; set; }
        public bool is_master { get; set; }
        public ApLocation ap_location { get; set; }
    }
    
    public class Msg
    {
        public ApEthMac ap_eth_mac { get; set; }
        public string ap_name { get; set; }
        public string ap_group { get; set; }
        public string ap_model { get; set; }
        public string depl_mode { get; set; }
        public ApIpAddress ap_ip_address { get; set; }
        public int ts { get; set; }
    }
    
    public class AccessPointResult
    {
        public Msg msg { get; set; }
    }
    
    public class RootObject
    {
        public List<AccessPointResult> Access_point_result { get; set; }
    }
    

    然后反序列化对象

    RootObject rootObject= new RootObject();
     rootObject= JsonConvert.DeserializeObject<RootObject>(jsonAgents);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2016-09-15
      • 2018-05-26
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多