在C#Webapi上使用多维数组反序列化JSON

时间:2017-05-31 08:36:26

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

我有一个带有多维数组的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; }
}

我怎样才能在很多结构方面打破反序列化?

2 个答案:

答案 0 :(得分: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#模型更改为camelcaseconvention,则必须在webapi配置中添加PascalCase 这是您的方法在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 }
            );
 }

答案 1 :(得分:0)

<强>模型

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; }
}

然后 DeserializeObject

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