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

React中如何解析JSON数组并移除所有反斜杠?

解决JSON数组值解析与反斜杠移除问题

你遇到的核心问题是把对象中存储为字符串形式的JSON数据,转换成实际可操作的数组/对象——你看到的反斜杠只是代码里字符串引号的转义符,实际对象中并不存在这些反斜杠,盲目处理整个对象的转义完全没必要。

看你提供的代码,fields_to_display、filter_fields、accounts这些字段都是JSON格式的字符串,正确的处理方式是把这些字符串单独解析成对应的数组/对象:

const jsonObject = {
  "account_create_view": [
    {
      "accounts": "[2092]",
      "fields_to_display": '["account_name", "company"]',
      "filter_fields": '[{"input": "account_name", "operator": "equals", "values": ["Prakruthi Resources - Saii Resources"]}]',
      "id": 184,
      "unique_name": "acc_12_18",
      "view_name": " acc_12_18"
    }
  ]
};

// 针对性解析每个条目中的JSON字符串字段
const cleanedObject = {
  ...jsonObject,
  account_create_view: jsonObject.account_create_view.map(item => ({
    ...item,
    // 解析为数组
    accounts: JSON.parse(item.accounts),
    fields_to_display: JSON.parse(item.fields_to_display),
    // 解析为对象数组
    filter_fields: JSON.parse(item.filter_fields)
  }))
};

console.log(cleanedObject);

关键说明

  • 遍历account_create_view数组,对目标字段执行JSON.parse,直接将字符串转成可操作的数组/对象
  • 保留原对象的所有其他字段,避免破坏原有数据结构
  • 如果后续新增类似的JSON字符串字段,只需要在map回调里补充对应的解析逻辑即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 18:39:52