I am querying an API using C# and the API returns a JSON array. I have it so that only the 1st set of information is returned, but I need all of the information to be returned. This is how the JSON data is returned.
And this is my C# syntax that I am using to display the dadta on screen. However, only the 1st set of data is displayed. not all of it. Meaning only the data for classID 2 is displayed, not the data for classid 3. How shoudl I alter this so that all of
the data is returned?
public class RootObject
{
public List<GradeList> GradeList { get; set; }
}
public class GradeList
{
public List<Dictionary<string, string>> grades { get; set; }
}
static void Main(string[] args)
{
foreach (RootObject ro in o)
if (ro.GradeList != null)
foreach (GradeList info in ro.GradeList)
{
var EI = info.grades.FirstOrDefault();
if (EI != null)
{
string studentID = EI["studentID"];
string classID = EI["classID"];
string grade = EI["grade"];
string PassorFail = EI["PorF"];
Console.WriteLine("The studentID is:" + studentID);
Console.WriteLine("The classID is:" + classID);
Console.WriteLine("The grade is:" + grade);
Console.WriteLine("Pass or Fail:" + PassorFail);
}
}
}
It is because you are specifying yourself to just pick the First record, you need to change your code to iterate all grades objects in the array, so change the following line:
Member
46 Points
124 Posts
Parse JSON Array
Jun 13, 2017 03:52 PM|LiarLiarPantsOnFire|LINK
I am querying an API using C# and the API returns a JSON array. I have it so that only the 1st set of information is returned, but I need all of the information to be returned. This is how the JSON data is returned.
And this is my C# syntax that I am using to display the dadta on screen. However, only the 1st set of data is displayed. not all of it. Meaning only the data for classID 2 is displayed, not the data for classid 3. How shoudl I alter this so that all of the data is returned?
Member
430 Points
122 Posts
Re: Parse JSON Array
Jun 13, 2017 05:00 PM|ehsansajjad465|LINK
It is because you are specifying yourself to just pick the First record, you need to change your code to iterate all grades objects in the array, so change the following line:
to :
Now it will print each grade object from the grades object.
Hope it helps!