无法将当前 JSON 对象(例如 {“name":“value"})反序列化为“System.Collections.Generic.List"类型

Cannot deserialize the current JSON object (e.g. {quot;namequot;:quot;valuequot;}) into type #39;System.Collections.Generic.List`1(无法将当前 JSON 对象(例如 {“name:“value})反序列化为“System.Collections.Generic.List类型)
本文介绍了无法将当前 JSON 对象(例如 {“name":“value"})反序列化为“System.Collections.Generic.List"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用系统;使用 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"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)