0

Unable to create mapping object / response model for the received JSON response from the service.

Tried of creating custom JsonConverter but could not find any appropriate method which can help to resolve the Deserializing issue

The following is the JSON response which i am getting from the Service.

{
    "$base": "Collection",
    "nodeType": "PROTOCOL",
    "Smart Building 0": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    },
    "Smart Building 1": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    },
    "Smart Building 2": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    }
}

Example 1:- Same service can return below JSON format

{
    "$base": "Collection",
    "nodeType": "PROTOCOL",
    "Smart Building 0": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    },
    "Smart Building 1": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    }
}

Create c# root object class so that it can easily used to Deserializing using JsonConvert

Hamid Shaikh
  • 197
  • 6
  • I what you are trying to do will generally be very difficult, as the provided JSON isn't in a list format either but rather seperate objects called "Smart Building 1" etc. if you have controll over the JSON wouldn't it be easier to reformat the JSON rather then having a complicated server side deserializer? – NinoM Jul 10 '19 at 13:35
  • @NinoMemelink I agree on your provided suggestion but i don't have control over JSON response format sent back from the service... – Hamid Shaikh Jul 10 '19 at 13:38

1 Answers1

0

Try this one:

public class Root : Dictionary<string, Building>
{   
    [JsonProperty("$base")]
    public string Base { get; set; }

    [JsonProperty("nodeType")]
    public string NodeType { get; set; }
}

public class Building
{
    [JsonProperty("$base")]
    public string Base { get; set; }

    [JsonProperty("nodeType")]
    public string NodeType { get; set; }

    [JsonProperty("truncated")]
    public string Truncated { get; set; }
}
mtkachenko
  • 5,389
  • 9
  • 38
  • 68