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.

9
  • 2
    Your question is unclear. When you say "do not create", what does that mean? Commented Dec 19, 2014 at 7: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 Commented Dec 19, 2014 at 7: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. Commented Dec 19, 2014 at 8:04
  • Are you sure the dictionary contains all the values you're expecting? Commented Dec 19, 2014 at 8:12
  • @YuvalItzchakov, yes i see them in the VS debugger. The whole debugger has this look i.imgur.com/GPKESTU.png The filter variable in the debugger is listed here as json for better readability Commented Dec 19, 2014 at 8:22

0

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.