The point of the program is to find the total number of films a character has been a part of.
I'm trying to access a Newtonsoft.Json.Linq.JArray
type object to count the length of a multidimensional array.
Json array:
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "descriptiontext",
"films": [
"linkapifilms1",
"linkapifilms2",
"linkapifilms3",
"linkapifilms6",
"linkapifilms7"
],
"species": [],
"vehicles": [
"linapivehicles14",
"linapivehicles30"
],
"starships": [
"linapistarships12",
"linapistarships22"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "linkapipeople1"
}
]
}
I'm trying to access "films":
to count the length of the array and store/display it on an int variable.
My Code:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Solution
{
static void Main()
{
Console.WriteLine("Number of films: " + Run("Luke Skywalker"));
Console.ReadKey();
}
static public int Run(string character)
{
int numberOfFilms = 0;
Task<string> result = GetResponseString(character);
var jsonResult = result.Result;
dynamic dynamicResultObject = JsonConvert.DeserializeObject(jsonResult);
JArray x = dynamicResultObject.results;
//Find length of "films" code insert//
Console.WriteLine(character);
return numberOfFilms;
}
static public async Task<string> GetResponseString(string character)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("linktoapi/api/people/?search=" + character);
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
}
I can access the first line of array with dynamicResultObject.results;
So the question is how do I access films, which is inside results and find the length?
Edit: I understand that this may not be the best way to go about writing this program however, is it not possible to just access the child array of the main array with the current code?