0

I have a webservice that return a JSON in this format:

[{
    "Route0": {
        "RouteID": "AAA",
        "RouteDescription": "",
        "ReturnCode": "0",
        "ReturnError": ""
    }
}, {
    "Route1": {
        "RouteID": "AABCLO",
        "RouteDescription": "Antoine Abdo Bachaalani Close",
        "ReturnCode": "0",
        "ReturnError": ""
    }
}]

I need to deserialize it: I created 2 classes:

public class PullRouteDetailsObjectChild
    {
        public string RouteID { get; set; }
        public string RouteDescription { get; set; }
        public string ReturnCode { get; set; }
        public string ReturnError { get; set; }
    }

    public class PullRouteDetailsObject
    {
        public PullRouteDetailsObjectChild Route { get; set; }
    }

and I am using this code to deserialize:

    List<PullRouteDetailsObject> jsonRoutes =      
JsonConvert.DeserializeObject<List<PullRouteDetailsObject>>(jsonresult);

I am able to get a list of 2 PullRouteDetailsObject which is correct but the child object is always null. I am sure that I am missing something but can't find what. I need to access child object

Thank you for your help.

1 Answer 1

1

You need to use this instead:

var jsonRoutes = JsonConvert.DeserializeObject<List<Dictionary<string, PullRouteDetailsObject>>>(jsonresult);

As you're getting a list of objects with a property which contains your 'PullRouteDetailsObject' (Route0, Route1).

Sign up to request clarification or add additional context in comments.

1 Comment

@chahid jsonRoutes.First(r => r.ContainsKey("Route1"))["Route1"] (for what it's worth, I don't think the structure of that json helps anybody, client or server)

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.