0

I'm parsing a json which has 3 properties that have arrays as values, to my controller. I managed to save the json to a Dictionary, and now i need those 3 arrays to make a query to my database.

The setup is the following

public JsonResult FilterMultiselects(string json)
    {
        Dictionary<string, string[]> filters = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(json);
        string[] lines = filters["lines"];
        string[] countries = filters["countries"];
        string[] users = filters["users"];

        var query = ... //do my query which returns the results i desire
        string result = JsonConvert.SerializeObject(query, Formatting.Indented, 
                                                        new JsonSerializerSettings { 
                                                                PreserveReferencesHandling = PreserveReferencesHandling.Objects
                                                        });
        return Json(result, JsonRequestBehavior.AllowGet);
    }

The problem im having is that when i get to the part in which i initialize those 3 arrays only the second one(countries) exists. The other two dont even create which makes no sense to me. Even thought i have not worked with Dictionaries(hashes) too much in C# it looks good.

Could anyone help me with what am i missing? The json im parsing has the following look:

{
    lines:  ["line 1", "line 2"]
    countries: ["England"]
    users: []
}

The string my controller receives is:

"{\"lines\":[\"line 1\",\"line 2\"],\"countries\":[\"England\"],\"users\":[]}"

The dictionary gets created and has 3 keys that have the correct arrays as values.

dimitrov.l
  • 63
  • 1
  • 5
  • 2
    Your question is unclear. When you say "do not create", what does that mean? – Yuval Itzchakov Dec 19 '14 at 07:57
  • 1
    Why do you need to pass the string and then parse it? You can rely on model-binding for automatically parsing your values into a model – Vsevolod Goloviznin Dec 19 '14 at 07:59
  • @YuvalItzchakov the second array(countries) gets initialized like you would expect, but the first and third don't get even initialized, those arrays don't even exist in my context, which is really strange. – dimitrov.l Dec 19 '14 at 08:04
  • Are you sure the dictionary contains all the values you're expecting? – Yuval Itzchakov Dec 19 '14 at 08:12
  • @YuvalItzchakov, yes i see them in the VS debugger. The whole debugger has this look http://i.imgur.com/GPKESTU.png The filter variable in the debugger is listed here as json for better readability – dimitrov.l Dec 19 '14 at 08:22
  • @dimitrov.l you have all three arrays initialized there, what's the problem? – Vsevolod Goloviznin Dec 19 '14 at 09:06
  • @VsevolodGoloviznin the arrays you see are the ones in the dictionary, i need to get them out of the dictionary to separate arrays – dimitrov.l Dec 19 '14 at 09:33
  • @dimitrov.l why can't you use `dict["countries"]`? – Vsevolod Goloviznin Dec 19 '14 at 09:34
  • I don't know whether this is related to your problem or not, but I notice you are double-serializing your query result: first you use `JsonConvert.SerializeObject()`, then you use the `Json()` method, which serializes it a second time. You should be using the `Content()` method instead to create your `ActionResult`. See [this answer](http://stackoverflow.com/a/21939378/10263). – Brian Rogers Dec 19 '14 at 18:44

0 Answers0