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

如何从d.body中提取data数组的首个对象数据?

问题:提取JSON中的指定data值

我现在思路卡顿,需要提取指定的"data"值。console.log(d.body)的输出如下:

{
    "data": [{
        "id": "30220059",
        "login": "esl_sc2",
        "display_name": "ESL_SC2",
        "type": "",
        "broadcaster_type": "partner",
        "description": "For standings, schedule, and results, visit https://pro.eslgaming.com/tour/sc2/",
        "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/cac110f7-0711-421f-9341-c77b53ab63b5-profile_image-300x300.jpeg",
        "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/ac0e240d-63c2-4faf-9040-60543b3e2c0f-channel_offline_image-1920x1080.jpeg",
        "view_count": 0,
        "created_at": "2012-05-02T09:59:20Z"
    }]
}

想要得到如下输出:

{
    "id": "30220059",
    "login": "esl_sc2",
    "display_name": "ESL_SC2",
    "type": "",
    "broadcaster_type": "partner",
    "description": "For standings, schedule, and results, visit https://pro.eslgaming.com/tour/sc2/",
    "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/cac110f7-0711-421f-9341-c77b53ab63b5-profile_image-300x300.jpeg",
    "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/ac0e240d-63c2-4faf-9040-60543b3e2c0f-channel_offline_image-1920x1080.jpeg",
    "view_count": 0,
    "created_at": "2012-05-02T09:59:20Z"
}

我原本以为直接用d.body.data[0]就行,这个方法对吗?


解答

你的思路完全正确,d.body.data[0]就是获取目标对象的正确方式。

需要注意一个前提:确保d.body已经是解析后的JavaScript对象,而不是原始的JSON字符串。如果d.body还是字符串格式,得先通过JSON.parse(d.body)把它转换成对象,再用data[0]提取内容,示例代码如下:

const parsedBody = JSON.parse(d.body);
const targetData = parsedBody.data[0];
console.log(targetData);

如果d.body本身已经是对象(比如你使用的HTTP库自动帮你解析了响应),那直接用d.body.data[0]就能得到你想要的结果。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 12:57:28