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

I am getting above error for foreach line

Harshada
  • 3
  • 1
  • 4
  • https://app.quicktype.io/#l=cs&r=json2csharp - 90% of your JSON parsing problems gone via auto-generation to safety! – mjwills Apr 07 '20 at 08:14
  • 2
    Does this answer your question? [Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'](https://stackoverflow.com/questions/33495634/unable-to-cast-object-of-type-newtonsoft-json-linq-jobject-to-type-newtonsoft). – RoadRunner Apr 07 '20 at 08:15
  • @mjwills Could also use `Edit -> Paste Special -> Paste JSON as Classes` in Visual Studio. – RoadRunner Apr 07 '20 at 08:24
  • 1
    Yep, not quite as nice (in terms of casing etc) but that does also work @RoadRunner. – mjwills Apr 07 '20 at 08:24

1 Answers1

1

JArray is containting an array of JObject in this case so you have JObject collection in the JArray your loop should look like:

foreach(JObject ja in userResponseArray)
{
   string yourKey = item.GetValue("yourKey").ToString();
}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • `JArray` is an array of `JToken` see [the docs](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm) and it can contain anything like `JArray`, `JObject`, `JValue`, ... – Sir Rufo Apr 07 '20 at 08:28
  • @SirRufo yes but in this case it is containing an array of `JObject`, i edited the post, Thanks for the correction – Ehsan Sajjad Apr 07 '20 at 10:20