3

I have the following dictionary:

   [JsonProperty("Simulations")]
    public IDictionary<int, Simulation> Simulations { get; set; }

Actual behavior:

When I'm sending my data to the front I send it as an object:

   "simulations": {
       "02": {
            "rachatBrut": 542,
            "montantPercu": 250,
            },
        "52": {
            "rachatBrut": 400,
            "montantPercu": 385,
            },
    }

Wanted Behavior

I want to send only the values of the dictionary as an array:

   "simulations": [
       {
            "rachatBrut": 542,
            "montantPercu": 250,
       },
       {
            "rachatBrut": 400,
            "montantPercu": 385,
       }
    ]
1
  • JsonIgnore the existing property. Create a new readonly property which projects the array you want in the JSON. Commented Feb 27, 2019 at 13:11

1 Answer 1

1

You could either add another property and use [JsonIgnore] on the one u have right now. To make the new one an array just call .ToArray() on the Dictionary. Or you can write yourself an own CustomJsonConverter for this behavior.

For your specific case this would produce an array like the one u need:

Simulations.Select(k => k.Value).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

It produces an array containing KeyValuePair Objects. If u want only the values u need to use a Select before.
Thank you. I ended up converting my dictionary into an array of simulations

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.