0

I have a JSON that I receive which has dynamic numeric object names. I´m not sure how to convert this into a model.

I have tried to create a model structure but I did not manage to solve it.

My JSON looks like this:

{
    "data_positions": {
        "695243": { <- this is a dynamic number which can be different for another request
            "id": "695243",
            "name": "Gothenburg",
            "numberOfPos": "59865",
            "partners": {
                "9658265": {
                    "id": "9658265",
                    "partner": {
                        "id": "1",
                        "name": "FirstOne"
                    }
                },
                "2365895": { <- this is a dynamic number which can be different for another request
                    "id": "2365895",
                    "participant": {
                        "id": "2",
                        "name": "SecondOne",
                    }
                },
                "1478563": { <- this is a dynamic number which can be different for another request
                    "id": "1478563",
                    "participant": {
                        "id": "3",
                        "name": "ThirdOne"
                    }
                }
            }
        }
    }
}

And the model I have created looks like this:

public class Data
{
    [JsonProperty("data_positions")]
    public DataPositions DataPositions { get; set; }
}

public class Data
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("numberOfPos")]
    public int NumberOfPos { get; set; }

    [JsonProperty("partners")]
    public Partner[] Partners { get; set; }
}

public class Partner
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("numberOfPos")]
    public int NumberOfPos { get; set; }
}

So what I´m unsure of is how to fetch the data from the object 695243 under data_positions and 2365895, 1478563 in partners.

The expected result is a model that can fetch the data from the JSON provided above.

Appreciate all help.

1 Answers1

0

The easiest thing you can do is just use Dictionary<,>, eg. Dictionary<string, DataPosition>.

Assuming you made a typo in the model and second class should be named DataPosition and ignoring the structure of DataPositions, the model would look like this:

public class Data
{
    [JsonProperty("data_positions")]
    public Dictionary<string, DataPosition> DataPositions { get; set; }
}

public class DataPosition
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("numberOfPos")]
    public int NumberOfPos { get; set; }

    [JsonProperty("partners")]
    public Dictionary<string, Partner> Partners { get; set; }
}

public class Partner
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("numberOfPos")]
    public int NumberOfPos { get; set; }
}

You can just access the entry in the dictionary by key, use .First() or iterate through them, depending on the data.

PS. The structure for the Partner type does not match the JSON, you might want to fix that.

kiziu
  • 1,111
  • 1
  • 11
  • 15
  • it´s working now thanks, but I had to do a middle step between the `Partners dictionary` and the `Partner` to be able to fetch the `Partner data`. Or did I miss anything with your answer? – user12031252 Sep 06 '19 at 16:05