問題描述
使用系統;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用臉書;使用 Newtonsoft.Json;命名空間臉書{課堂節目{靜態無效主要(字符串 [] 參數){var client = new FacebookClient(acc_ess);動態結果 = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"});字符串 jsonstring = JsonConvert.SerializeObject(result);//jsonstring {"data":[{"target_id":9503123,"target_type":"user"}]}列出<根對象>datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);}公共類基準{公共 Int64 target_id { 獲取;放;}公共字符串目標類型 { 獲取;放;}}公共類 RootObject{公共列表<基準>數據{得到;放;}}}}
<塊引用>
無法反序列化當前 JSON 對象(例如 {"name":"value"})輸入類型'System.Collections.Generic.List`1[facebook.Program+RootObject]'因為該類型需要一個 JSON 數組(例如 [1,2,3])來反序列化正確.要修復此錯誤,請將 JSON 更改為 JSON 數組(例如 [1,2,3])或更改反序列化類型,使其成為正常的.NET 類型(例如,不是像整數這樣的原始類型,也不是集合類型如數組或列表)可以是
我看了其他帖子.
我的 json 看起來像這樣:
{"data":[{"target_id":9503123,"target_type":"user"}]}
為了明確,除了@SLaks 的回答,這意味著你需要改變這一行:
Listdatalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);
到這樣的事情:
RootObject datalist = JsonConvert.DeserializeObject(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"類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!