首页 > 解决方案 > 出现错误无法将“Newtonsoft.Json.Linq.JObject”类型的对象转换为“Newtonsoft.Json.Linq.JArray”类型

问题描述

JObject joResponse = JObject.Parse(response);
JObject ojObject = (JObject)joResponse["d"];
JArray userResponseArray = (JArray)ojObject["results"];
foreach(JArray ja in userResponseArray)
{
----
}

对于 foreach 行,我遇到了错误

标签: c#

解决方案


JArrayJObject在这种情况下包含一个数组,因此您JObject在 JArray 中有集合,您的循环应如下所示:

foreach(JObject ja in userResponseArray)
{
   string yourKey = item.GetValue("yourKey").ToString();
}

推荐阅读