c# - 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'Sy

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

我看了其他帖子。

我的 json 看起来像这样:

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

最佳答案

为了清楚起见,除了@SLaks 的回答之外,这意味着您需要更改这一行:

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

到这样的事情:

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

关于c# - 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List` 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358493/

相关文章:

json - 在 JSON 中禁用超文本应用程序语言 (HAL)?

json - 对于 Restful API,GET 方法可以使用 json 数据吗?

php - 在 Laravel 4 中将 Eloquent 模型返回为 JSON

json - 返回 Mvc.JsonResult 加上设置 Response.StatusCode

json - 如何解析嵌套 JSON 对象中的内部字段

ruby-on-rails - 什么是 JSON 八位组,为什么需要两个?

python - 用 json 编码元组的最佳方法

json - Jackson json反序列化,忽略json中的根元素

java - 将 JSONObject 转换为 Map

json - 使用 d3.json 导入本地 json 文件不起作用