Reputation: 4975
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.
{
"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.
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;
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?
Upvotes: 0
Views: 1342
Reputation: 129827
First of all, I would advise against using dynamic
as it will just cause you headaches. If you're going to work with JObjects
and JArrays
anyway you're better off just using those types directly. The compiler will be able to detect errors earlier and you will get the Intellisense help in Visual Studio. So, parse your JSON like this:
JObject rootObject = JObject.Parse(jsonResult);
From that you can get your results array. (You were calling this array x
in your code, but I think resultsArray
is a more descriptive variable name):
JArray resultsArray = (JArray)rootObject["results"];
Like any array, if you want to get at a particular element you need to use its index. We can get the first element of the array (which is the object containing Luke Skywalker's data) like this:
JObject characterObject = (JObject)resultsArray[0];
From there, you can get the films
property, which is also a JArray
:
JArray filmsArray = (JArray)characterObject["films"];
To get the length of a JArray
you use the Count
property:
numberOfFilms = filmsArray.Count;
Fiddle: https://dotnetfiddle.net/iFAWB4
Now that you know how to drill down through the objects and arrays step-by-step, I'll show you a little shortcut, the SelectToken()
method. This method allows you to specify a "path" to get a particular JToken
(JObject
and JArray
are both types of JTokens
). So, after parsing your JSON, you could get the films
array directly like this:
JArray filmsArray = (JArray)rootObject.SelectToken("results[0].films");
and then get its count just like before.
Fiddle: https://dotnetfiddle.net/zOdSFs
Upvotes: 4