164

I have this JSON:

[
    {
        "Attributes": [
            {
                "Key": "Name",
                "Value": {
                    "Value": "Acc 1",
                    "Values": [
                        "Acc 1"
                    ]
                }
            },
            {
                "Key": "Id",
                "Value": {
                    "Value": "1",
                    "Values": [
                        "1"
                    ]
                }
            }
        ],
        "Name": "account",
        "Id": "1"
    },
    {
        "Attributes": [
            {
                "Key": "Name",
                "Value": {
                    "Value": "Acc 2",
                    "Values": [
                        "Acc 2"
                    ]
                }
            },
            {
                "Key": "Id",
                "Value": {
                    "Value": "2",
                    "Values": [
                        "2"
                    ]
                }
            }
        ],
        "Name": "account",
        "Id": "2"
    },
    {
        "Attributes": [
            {
                "Key": "Name",
                "Value": {
                    "Value": "Acc 3",
                    "Values": [
                        "Acc 3"
                    ]
                }
            },
            {
                "Key": "Id",
                "Value": {
                    "Value": "3",
                    "Values": [
                        "3"
                    ]
                }
            }
        ],
        "Name": "account",
        "Id": "2"
    }
]

And I have these classes:

public class RetrieveMultipleResponse
{
    public List<Attribute> Attributes { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }
}

public class Value
{
    [JsonProperty("Value")]
    public string value { get; set; }
    public List<string> Values { get; set; }
}

public class Attribute
{
    public string Key { get; set; }
    public Value Value { get; set; }
}

I am trying to deserialize the above JSON using the code below:

var objResponse1 = JsonConvert.DeserializeObject<RetrieveMultipleResponse>(JsonStr);

but I am getting this error:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'test.Model.RetrieveMultipleResponse' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

dbc
  • 104,963
  • 20
  • 228
  • 340
Avinash
  • 2,053
  • 2
  • 14
  • 32

6 Answers6

257

Your json string is wrapped within square brackets ([]), hence it is interpreted as array instead of single RetrieveMultipleResponse object. Therefore, you need to deserialize it to type collection of RetrieveMultipleResponse, for example :

var objResponse1 = 
    JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);
har07
  • 88,338
  • 12
  • 84
  • 137
  • 1
    I had to remove "//" befor "[", then it worked for me. thanks – garish Aug 10 '17 at 13:49
  • 2
    If you wanted to keep it as a single object instead of a collection, could you just do JsonStr.Replace("[","").Replace("]","") or would this not be good practice? – Rich Apr 13 '18 at 18:34
  • 5
    FYI this didn't work for me because I was getting my JSON from an API and I had the freaking URL wrong for an entire day. > – w00ngy Oct 05 '18 at 13:14
  • 3
    What is RetrieveMultipleResponse here? – Freakishly Dec 26 '18 at 21:50
  • Thanks this worked for me var objResponse1 = JsonConvert.DeserializeObject>(srt); // myWord = myQuestionData.Word; Debug.Log("myWord" + objResponse1[0].Word); – StackBuddy Jul 06 '19 at 11:55
  • This may help someone.You can just get the data using JsonConvert.DeserializeObject(yourResponseObject) and then loop through it based on your need – Deepak Sep 04 '20 at 11:35
  • For what it's worth, I ended up here with the same error but it turned out I'd missed the await keyword from the async function call that returned the results. – Stu Price Mar 10 '22 at 19:25
  • This solution is not working when serialized from DataTable and then deserializing back to DataTable. Getting the same error when deserializing because the JSON is an array. – Aamir Apr 05 '22 at 22:19
16

If one wants to support Generics (in an extension method) this is the pattern...

public  static List<T> Deserialize<T>(this string SerializedJSONString)
{
    var stuff = JsonConvert.DeserializeObject<List<T>>(SerializedJSONString);
    return stuff;
}

It is used like this:

var rc = new MyHttpClient(URL);
//This response is the JSON Array (see posts above)
var response = rc.SendRequest();
var data = response.Deserialize<MyClassType>();

MyClassType looks like this (must match name value pairs of JSON array)

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
 public class MyClassType
 {
    [JsonProperty(PropertyName = "Id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "Description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "Manager")]
    public string Manager { get; set; }

    [JsonProperty(PropertyName = "LastUpdate")]
    public DateTime LastUpdate { get; set; }
 }

Use NUGET to download Newtonsoft.Json add a reference where needed...

using Newtonsoft.Json;
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
JWP
  • 6,672
  • 3
  • 50
  • 74
6

Can't add a comment to the solution but that didn't work for me. The solution that worked for me was to use:

var des = (MyClass)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(MyClass)); 
return des.data.Count.ToString();

Deserializing JSON array into strongly typed .NET object

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
TheNerdyNerd
  • 237
  • 2
  • 9
3

Use this, FrontData is JSON string:

var objResponse1 = JsonConvert.DeserializeObject<List<DataTransfer>>(FrontData);  

and extract list:

var a = objResponse1[0];
var b = a.CustomerData;
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
udorb b
  • 135
  • 3
-1

To extract the first element (Key) try this method and it will be the same for the others :

        using (var httpClient = new HttpClient())
        {
            using (var response = await httpClient.GetAsync("Your URL"))
            {
                var apiResponse = await response.Content.ReadAsStringAsync();
                var list = JObject.Parse(apiResponse)["Attributes"].Select(el => new {  Key= (string)el["Key"] }).ToList();
                var Keys= list.Select(p => p.Key).ToList();
            }
        }
-3
var objResponse1 = 
JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);

worked!

Maya
  • 75
  • 1
  • 3