Reputation: 3
I have a problem, and my problem is the following: I have this code in C# and I don't know how to do a foreach to get all pakFilename
WebClient client = new WebClient();
string strpagecode = client.DownloadString("https://fortnite-api.com/v2/aes");
dynamic dobj = JsonConvert.DeserializeObject<dynamic>(strpagecode);
string description = dobj["data"]["dynamicsKeys"][0]["pakFilename"].ToString();
aes.Text = description;
Could someone help me?
Thank you very much in advance
Upvotes: 0
Views: 61
Reputation: 9839
Maybe it would help to use real objects instead of dealing with dynamic
.
Generate c# classes from the JSON. Paste your json string to a tool like https://json2csharp.com/.
The following classes are generated:
public class DynamicKey
{
public string PakFilename { get; set; }
public string PakGuid { get; set; }
public string Key { get; set; }
}
public class Data
{
public string Build { get; set; }
public string MainKey { get; set; }
public List<DynamicKey> DynamicKeys { get; set; }
public DateTime Updated { get; set; }
}
public class Root
{
public int Status { get; set; }
public Data Data { get; set; }
}
Afterwards you could easily deal with these objects:
WebClient client = new WebClient();
string strpagecode = client.DownloadString("https://fortnite-api.com/v2/aes");
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(strpagecode);
List<string>? pakFileNames = myDeserializedClass.Data.DynamicKeys.Select(x => x.PakFilename).ToList();
// add to textbox
aes.text = string.Join("\n", pakFileNames);
This should give you the list of all pakFileNames
:
Upvotes: 1
Reputation: 108
Is this what you mean?
var dynamicsKeys = dobj["data"]["dynamicsKeys"];
foreach(var dynamicsKey in dynamicsKeys) {
string pakFilename = dynamicsKeys["pakFilename"].ToString()
}
Upvotes: 1