0

I have a json string that i am trying to deserialize into an object list using Newton. But it gives the

error Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

The format of the json string is.

 [ { "key": "1", "value": "Package1" }, { "key": "some name", "value": "Package2" } ]

the Model is defined as

public class RootObject
{
    public string key { get; set; }
    public string value { get; set; }
}

And i'm using the following method to deserialize it, where json is the string listed above.

var resultList = JsonConvert.DeserializeObject<List<RootObject>>(json);

I'm not sure where i'm going wrong here, any help would be appreciated.

4
  • 7
    Are you sure this is the json text that you're passing to the Deserialize method ? Because it should work completely fine for me. Can you output json object to the Console and paste it here ? Commented Jul 21, 2016 at 18:28
  • 3
    Did you escape the quotes? var jsonData = "[ { \"key\": \"1\", \"value\": \"Package1\" }, { \"key\": \"some name\", \"value\": \"Package2\" } ]"; Commented Jul 21, 2016 at 18:33
  • 4
    what you have seems to work fine in fiddle: dotnetfiddle.net/CvKGau Commented Jul 21, 2016 at 18:36
  • Thanks Every for your help, Petre Turuc that was the issue. Once i escaped the quotes it worked! Commented Jul 22, 2016 at 4:40

1 Answer 1

1

Try giving IDictionary instead of List.

Sign up to request clarification or add additional context in comments.

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.