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.
3
  • Have you tried deserializing to an array of ExploreCriteria, ExploreCriteria[]? Because your outer most thing in your JSON is an array. Commented Sep 8, 2016 at 6:20
  • Similar to this stackoverflow.com/questions/17762032/… Commented Sep 8, 2016 at 6:21
  • try var explore = JsonConvert.DeserializeObject<Dictionary<String, List<String>>>(JsonStr) Commented Sep 8, 2016 at 6:50

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

@AdilWaqar - sorry the Explore property was not fitting. I have edited the code so that it is fitting to your ´ExploreCriteria´.
0

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

use keyvaluepair class instead of dictionary.

Comments

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.