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

JSON反序列化时出现重复对象问题求助

Drupal分页获取JSON数据出现重复对象问题

我从Drupal站点获取内容列表时遇到异常:通过GET请求分页拉取JSON数据并反序列化为对象后,总节点数正确(共1896个),但其中约51个是重复对象,且集中在dr_puppies数组的1248至1351索引区间。

已排查信息:

  • 源数据无重复、无异常;
  • 请求URL路径正常,偏移量每次递增100,limit固定为100;
  • 共处理约19页数据;
  • 核心问题是重复对象覆盖了数组中的有效对象,需从根源避免重复,而非仅做事后去重。

我未在数组生成阶段执行特殊逻辑,1248索引位置无特殊意义,怀疑是异步调用的问题,但此前从未出现过该情况。

相关代码如下:

private async Task RefreshPuppyData()
{
    dr_puppies = new Puppy();

    // 获取初始页JSON数据
    string initialJSON = await App.cmSession.LinkedSite.GetJSON("jsonapi/node/puppies?page[offset]=0&page[limit]=100");
    dr_puppies = JsonConvert.DeserializeObject<Puppy>(initialJSON);
    
    // 检查是否存在需要拉取的额外分页数据
    bool morePages = false;
    if (dr_puppies.links.next != null)
    {
        morePages = true;
    }

    // 存在"next"链接时,循环处理后续分页
    while (morePages)
    {
        Puppy comboData = new Puppy();
        Puppy dataRetrievalTemp = new Puppy();

        // 请求前修复"next" URL格式(将转义文本替换为方括号)
        StringBuilder builder = new StringBuilder(dr_puppies.links.next.href);
        builder.Replace("%5D", "]");
        builder.Replace("%5B", "[");
        builder.Replace("[limit]=50", "[limit]=100");
        builder.Replace("[offset]=50", "[offset]=100");
        dr_puppies.links.next.href = builder.ToString();

        // 获取下一页JSON数据
        string nextJSON = await App.cmSession.LinkedSite.GetJSON(dr_puppies.links.next.href);
        dataRetrievalTemp = JsonConvert.DeserializeObject<Puppy>(nextJSON);

        // 合并当前页与之前所有页的数据,复制到用户数据对象
        PuppyData[] oldData = dr_puppies.data;
        PuppyData[] newData = dataRetrievalTemp.data;
        comboData.links = dataRetrievalTemp.links;
        int newLen = oldData.Length + newData.Length;
        
        comboData.data = new PuppyData[newLen];
        oldData.CopyTo(comboData.data, 0);
        newData.CopyTo(comboData.data, oldData.Length);
        dr_puppies = comboData;

        // 无后续页面时退出循环
        if (dr_puppies.links.next == null)
        {
            morePages = false;
        }
    }

    // 检查列表中的重复项(出现次数大于1)
    for (int i = 0; i < dr_puppies.data.Length; i ++)
    {
        int matches = 0;
        for (int j = 0; j < dr_puppies.data.Length; j++)
        {
            if (dr_puppies.data[i].attributes.title == dr_puppies.data[j].attributes.title)
            {
                matches++;
            }
        }
        if (matches > 1)
        {
            Debug.WriteLine("重复条目:" + dr_puppies.data[i].attributes.title + ",索引位置:" + i);
        }
    }
}
public async Task<string> GetJSON(string getPath)
{
    try
    {
        Windows.Web.Http.HttpResponseMessage response;
        if (getPath.StartsWith("http"))
        {
            response = await Client.GetAsync(new System.Uri(getPath));
        } 
        else
        {
            response = await Client.GetAsync(new System.Uri(URL + getPath));
        }
        response.EnsureSuccessStatusCode();
        string jsonResponse = await response.Content.ReadAsStringAsync();
        return jsonResponse;
    } 
    catch (Exception ex)
    {
        Debug.WriteLine("获取站点JSON数据失败:" + ex.Message);
        return null;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 11:00:09