更新时间:2022-10-30 11:01:43
为了说清楚,除了@SLaks的答案,这意味着你需要改变这一行:
列表与LT; RootObject> datalist = JsonConvert.DeserializeObject< List< RootObject>>(jsonstring);
如下所示:
RootObject datalist = JsonConvert.DeserializeObject< RootObject>(jsonstring);
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"}]}
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);
无法将当前 JSON 对象(例如 {“name":“value"})反序列化为类型“System.Collections.Generic.List"1
无法将当前 JSON 对象(例如 {“name":“value"})反序列化为“System.Collections.Generic.List"类型
无法将当前的JSON对象反序列化(例如{“name”:“value”})转换为“System.Collections.Generic.List”1类型
无法将 JSON 数组(例如 [1,2,3])反序列化为类型"",因为类型需要 JSON 对象(例如 {“name":“value"})才能正确反序列化
'无法将当前JSON对象(例如{" Name":" value"})反序列化为类型'system.collections.generic.list`1 ...
无法将当前JSON对象(例如{"name":"value"})反序列化为"System.Collections.Generic.List"类型
JSON.Net-无法将当前json对象(例如{"name":"value"})反序列化为类型'system.collections.generic.list`1
无法将 JSON 数组(例如 [1,2,3])反序列化为类型“",因为类型需要 JSON 对象(例如 {“name":“value"})才能正确反序列化
无法将当前json对象反序列化,因为(例如。{name":" value"}})因为类型需要json数组(例如[1,2,3])