You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2019中Windows Forms项目的C# foreach遍历获取所有pakFilename问题求助

How to Loop Through All pakFilename Values in Your Fortnite API Response

Hey! I see you're trying to grab every pakFilename from the Fortnite API response instead of just the first one. Let's get this sorted out for you quickly.

Quick Fix with dynamic

Since you're already using dynamic to deserialize the JSON, you can directly iterate over the dynamicsKeys array with a foreach loop. Here's how to adjust your existing code:

WebClient client = new WebClient();
string strpagecode = client.DownloadString("https://fortnite-api.com/v2/aes");
dynamic dobj = JsonConvert.DeserializeObject<dynamic>(strpagecode);

// Clear the TextBox first to avoid mixing old and new content
aes.Text = string.Empty;

// Loop through every entry in the dynamicsKeys array
foreach (var keyEntry in dobj["data"]["dynamicsKeys"])
{
    string pakFileName = keyEntry["pakFilename"].ToString();
    // Append each filename to the TextBox with a newline for readability
    aes.Text += pakFileName + Environment.NewLine;
}

This code goes through every element in the dynamicsKeys collection, pulls out the pakFilename from each entry, and adds it to your aes TextBox.

More Robust Approach: Strongly-Typed Classes

While dynamic works for quick tests, it doesn't give you compile-time error checks or IntelliSense—easy to mistype a property name and break things later. A better practice is to define classes that match the API's JSON structure, then deserialize into those classes:

First, create the model classes:

public class AesApiResponse
{
    public AesData data { get; set; }
}

public class AesData
{
    public List<DynamicAesKey> dynamicsKeys { get; set; }
}

public class DynamicAesKey
{
    public string pakFilename { get; set; }
    // Add other properties from the API here (like "key" or "name") if you need them
}

Then update your main code:

WebClient client = new WebClient();
string strpagecode = client.DownloadString("https://fortnite-api.com/v2/aes");
AesApiResponse apiResponse = JsonConvert.DeserializeObject<AesApiResponse>(strpagecode);

aes.Text = string.Empty;

foreach (var key in apiResponse.data.dynamicsKeys)
{
    aes.Text += key.pakFilename + Environment.NewLine;
}

With this approach, Visual Studio will give you IntelliSense for the properties, and any typos will be caught at compile time instead of runtime.

Bonus: Modernize with HttpClient

Note that WebClient is obsolete in newer .NET versions. For a more efficient, async-friendly approach, use HttpClient instead. Just mark your event handler as async:

private async void FetchAesKeysButton_Click(object sender, EventArgs e)
{
    aes.Text = "Loading...";
    
    using (HttpClient client = new HttpClient())
    {
        try
        {
            string strpagecode = await client.GetStringAsync("https://fortnite-api.com/v2/aes");
            AesApiResponse apiResponse = JsonConvert.DeserializeObject<AesApiResponse>(strpagecode);
            
            aes.Text = string.Empty;
            foreach (var key in apiResponse.data.dynamicsKeys)
            {
                aes.Text += key.pakFilename + Environment.NewLine;
            }
        }
        catch (Exception ex)
        {
            aes.Text = $"Error: {ex.Message}";
        }
    }
}

This adds basic error handling too, so your app won't crash if there's a network issue or API error.

内容的提问来源于stack exchange,提问作者salvatore20553

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 16:43:11