0

I have a sample Json

[{"_2":["HR Data","Reformed (Master File)"]}]

and I am trying to deserialize it into below model

public class ExploreCriteria
    {
        public Dictionary<String, List<String>> Explore { get; set; }
    }

this is what I have tried so far

ExploreCriteria Explore = new ExploreCriteria();
Explore = JsonConvert.DeserializeObject<ExploreCriteria>(JsonStr);

but it says

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'DataModels.ExploreCriteria' because the type requires a JSON object
(e.g. {"name":"value"}) to deserialize correctly.
AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • Have you tried deserializing to an array of `ExploreCriteria`, `ExploreCriteria[]`? Because your outer most thing in your JSON is an array. – ZoolWay Sep 08 '16 at 06:20
  • Similar to this http://stackoverflow.com/questions/17762032/cannot-deserialize-the-current-json-array-e-g-1-2-3-into-type – Sumit Parakh Sep 08 '16 at 06:21
  • try `var explore = JsonConvert.DeserializeObject>>(JsonStr)` – adiga Sep 08 '16 at 06:50

2 Answers2

6

The provided JSON and your ExploreCriteria class do not describe the same structure.

Your JSON structure is an array that contains a key with an array value. So you can either remove the square brackets to

{"_2":["HR Data","Reformed (Master File)"]}

then your ExploreCriteria is fitting. Or you can change the JsonConvert call to

var JsonStr = "[{\"_2\":[\"HR Data\",\"Reformed(Master File)\"]}]";
ExploreCriteria Explore = new ExploreCriteria();
var data = JsonConvert.DeserializeObject<IEnumerable<Dictionary<String, List<string>>>>(JsonStr);
Explore.Explore = data.FirstOrDefault();
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
  • 1
    @AdilWaqar - sorry the Explore property was not fitting. I have edited the code so that it is fitting to your ´ExploreCriteria´. – Ralf Bönning Sep 08 '16 at 07:00
1

List<KeyValuePair<string, List<string>>> uploadedfiles = JsonConvert.DeserializeObject<List<KeyValuePair<string, List<string>>>>(json);

use keyvaluepair class instead of dictionary.

Divya
  • 373
  • 4
  • 3