Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"}]} 
like image 483
user3236519 Avatar asked Jan 26 '14 01:01

user3236519


People also ask

Can not deserialize JSON?

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<T> that can be deserialized from a JSON array.

Can T Deserialize JSON array into?

bind. JsonbException: Can't deserialize JSON array into: class exception occurs when you try to convert json string data to a java object. If the json string contains an array of objects and attempts to deserialize the json string to a json object, the array of objects could not be assigned to a single java object.

What is Deserializing JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How do you represent an array of objects in JSON?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.


1 Answers

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); 
like image 173
har07 Avatar answered Sep 19 '22 15:09

har07