无法将当前的JSON对象反序列化(例如{“name”:“value”})转换为“System.Collections.Generic.List”1类型 [英] Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1

查看:3357
本文介绍了无法将当前的JSON对象反序列化(例如{“name”:“value”})转换为“System.Collections.Generic.List”1类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用Facebook;
使用Newtonsoft.Json;

命名空间facebook
{
类程序
{
static void Main(string [] args)
{
var client =新的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}]}
列表< RootObject> datalist = JsonConvert.DeserializeObject< List< RootObject>>(jsonstring);
}

public class Datum
{
public Int64 target_id {get;组; }
public string target_type {get;组; }
}

public class RootObject
{
public List< Datum>数据{get;组; }
}
}
}




无法反序列化当前的JSON对象(例如{name:value})
into
'System.Collections.Generic.List`1 [facebook.Program + RootObject]'
因为类型需要一个JSON数组(例如[1,2,3])来正确反序列化
。要修复此错误,请将JSON更改为JSON数组
(例如[1,2,3])或更改反序列化类型,以使其成为正常的
.NET类型(例如,不是原始类型像整数,不是集合
类型像数组或列表)可以


我看了其他帖子。 / p>

我的json看起来像这样:

  {data {target_id:9503123target_type:user}}} 


解决方案

为了说清楚,除了@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类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆