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

C#JsonFacebookjson.net

C# Problem Overview


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using Newtonsoft.Json;

namespace facebook
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new FacebookClient(acc_ess);
            dynamic result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"});
            string jsonstring = JsonConvert.SerializeObject(result);

            //jsonstring {"data":[{"target_id":9503123,"target_type":"user"}]}
            List<RootObject> datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);
        }
        
        public class Datum
        {
            public Int64 target_id { get; set; }
            public string target_type { get; set; }
        }

        public class RootObject
        {          
            public List<Datum> data { get; set; }
        }
    }
}

> Cannot deserialize the current JSON object (e.g. {"name":"value"}) > into type > 'System.Collections.Generic.List`1[facebook.Program+RootObject]' > because the type requires a JSON array (e.g. [1,2,3]) to deserialize > correctly. To fix this error either change the JSON to a JSON array > (e.g. [1,2,3]) or change the deserialized type so that it is a normal > .NET type (e.g. not a primitive type like integer, not a collection > type like an array or List) that can be

I looked at other posts.

My json looks like this:

{"data":[{"target_id":9503123,"target_type":"user"}]}

C# Solutions


Solution 1 - C#

To make it clear, in addition to @SLaks' answer, that meant you need to change this line :

List<RootObject> datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);

to something like this :

RootObject datalist = JsonConvert.DeserializeObject<RootObject>(jsonstring);

Solution 2 - C#

As the error message is trying very hard to tell you, you can't deserialize a single object into a collection (List<>).

You want to deserialize into a single RootObject.

Solution 3 - C#

That happened to me too, because I was trying to get an IEnumerable but the response had a single value. Please try to make sure it's a list of data in your response. The lines I used (for api url get) to solve the problem are like these:

HttpResponseMessage response = await client.GetAsync("api/yourUrl");

if (response.IsSuccessStatusCode)
{
    IEnumerable<RootObject> rootObjects =
        awaitresponse.Content.ReadAsAsync<IEnumerable<RootObject>>();

    foreach (var rootObject in rootObjects)
    {
        Console.WriteLine(
            "{0}\t${1}\t{2}",
            rootObject.Data1, rootObject.Data2, rootObject.Data3);
    }

    Console.ReadLine();
}

Hope It helps.

Solution 4 - C#

Can you try to change your json without data key like below?

[{"target_id":9503123,"target_type":"user"}]

Solution 5 - C#

The real problem is that you are using dynamic return type in the FacebookClient Get method. And although you use a method for serializing, the JSON converter cannot deserialize this Object after that.

Use insted of:

dynamic result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"}); 
string jsonstring = JsonConvert.SerializeObject(result);

something like that:

string result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"}).ToString();

Then you can use DeserializeObject method:

var datalist = JsonConvert.DeserializeObject<List<RootObject>>(result);

Hope this helps.

Solution 6 - C#

public partial class tree
{
    public int id { get; set; }
    public string name { get; set; }
    public string sciencename { get; set; }
    public int familyid { get; set; }
}


private async void PopulateDataGridView()
{
    //For Single object response
    tree treeobj = new tree();
    treeobj = JsonConvert.DeserializeObject<tree>(Response);
    //For list of object response
    List<tree> treelistobj = new List<tree>();
    treelistobj = JsonConvert.DeserializeObject<List<tree>>(Response);
    //done
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3236519View Question on Stackoverflow
Solution 1 - C#har07View Answer on Stackoverflow
Solution 2 - C#SLaksView Answer on Stackoverflow
Solution 3 - C#Nelson ParraView Answer on Stackoverflow
Solution 4 - C#ddagsanView Answer on Stackoverflow
Solution 5 - C#RawsonView Answer on Stackoverflow
Solution 6 - C#SaravananView Answer on Stackoverflow