0

I have a JSON with multidimensional array. I have no idea how to deserialize it on my c# model. I did a very simple way of deserialization which is not working. I need to know how to deserialize my JSON on more structural way.

This is my json data

{
"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
  }
}
]
}

Below is my C# model

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

How can i make the break the deserialization much structural way?

1
  • Where is the multidimensional array? Commented May 31, 2017 at 9:22

2 Answers 2

1

you have to change your model to the following

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

and your webapi method should look like the following

public void Post(RootObject rootObject)

Note that you have to add camelcaseconvention in your webapi config if you want to change your c# model to PascalCase Here how your method should look like in your 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 }
            );
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Model

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

then DeserializeObject

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.