Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JSON反序列化问题_C#_.net_Json_Json.net - Fatal编程技术网

C# JSON反序列化问题

C# JSON反序列化问题,c#,.net,json,json.net,C#,.net,Json,Json.net,我得到了一个JSON响应,例如: JSON "meta": { "code": 200 }, "data": [ { "username": "username here", "bio": "bio here", "website": "web site here", "profile_picture": "link here", "full_name": "name here", "id": "id

我得到了一个JSON响应,例如:

JSON

"meta": {
    "code": 200
  },
  "data": [
    {
      "username": "username here",
      "bio": "bio here",
      "website": "web site here",
      "profile_picture": "link here",
      "full_name": "name here",
      "id": "id here"
    }
  ]
C#

公共类元数据
{
公共整数代码{get;set;}
}
公共类数据
{
公共字符串用户名{get;set;}
公共字符串bio{get;set;}
公共字符串网站{get;set;}
公共字符串配置文件\u图片{get;set;}
公共字符串全名{get;set;}
公共字符串id{get;set;}
}
公共类根对象
{
公共元{get;set;}
公共列表数据{get;set;}
}
我写了这段代码:

JObject instaCall = JObject.Parse(response);
Datum searchResult = instaCall["data"].ToObject<Datum>();
JObject instaCall=JObject.Parse(响应);
Datum searchResult=instaCall[“数据”].ToObject();
但会产生一个错误:

无法将当前JSON数组(例如[1,2,3])反序列化为类型 “WindowsFormsApplication1.functions.response+Datum”,因为类型 需要一个JSON对象(例如{“name”:“value”})来反序列化 没错

要修复此错误,请将JSON更改为JSON对象(例如。 {“name”:“value”})或将反序列化类型更改为数组或 实现集合接口的类型(例如ICollection、IList) 类似于可以从JSON数组反序列化的列表。 还可以将JsonArrayAttribute添加到类型中,以强制它 从JSON数组反序列化


正如错误试图告诉您的那样,
instaCall[“data”]
是一个数组。

您无法将其读入单个对象。

类似这样的内容如何:

var o = JsonConvert.DeserializeObject<RootObject>(response);
Datum searchResult = o.data.FirstOrDefault();

if (searchResult != null)
{
    // awesome
}
var o=JsonConvert.DeserializeObject(响应);
数据搜索结果=o.data.FirstOrDefault();
if(searchResult!=null)
{
//棒极了
}

在foreach中执行此操作?
var o = JsonConvert.DeserializeObject<RootObject>(response);
Datum searchResult = o.data.FirstOrDefault();

if (searchResult != null)
{
    // awesome
}