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

如何解析值为字符串形式数组的JSON?附目标JSON示例

如何解析包含JSON字符串的嵌套JSON?

这种情况我碰到过好几次,其实就是典型的「嵌套序列化JSON」问题——外层是标准的JSON结构,但其中某个字段的取值是被转成字符串的JSON数组。解决思路非常清晰:先解析外层JSON,拿到这个字符串字段后,再对它做一次JSON解析,就能得到你需要的数组结构了。

下面给你两种常用语言的具体实现:

Python 示例

import json

# 你的目标JSON字符串
raw_json = '''{ "vendorcategory": [ { "id": "ctg-1", "_cO": 1598431685530, "_dL": 1, "name": "Fruits & Vegetables", "value": "[ { \"id\":\"ctg-1A\", \"name\":\"Fresh Vegetables\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1A-1\", \"name\":\"Potato, Onion, Tomato\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-2\", \"name\":\"Leafy Vegetables\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-3\", \"name\":\"Root Vegetables\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-4\", \"name\":\"Cucumber & Capsicum\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-4\", \"name\":\"Cabbage & Cauliflower\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-5\", \"name\":\"Beans, Brinjals & Okra\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-6\", \"name\":\"Gourd, Pumpkin, Drumstick\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-7\", \"name\":\"Specialty\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1B\", \"name\":\"Herbs & Seasonings\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1B-1\", \"name\":\"Lemon, Ginger & Garlic\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1B-2\", \"name\":\"Indian & Exotic Herbs\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1C\", \"name\":\"Fresh Fruits\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1C-1\", \"name\":\"Mangoes\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-2\", \"name\":\"Bananna, Sopota & Papaya\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-3\", \"name\":\"Apples & Pomegranate\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-4\", \"name\":\"Kivi, Melon, Citrus fruit\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-5\", \"name\":\"Seasonal Fruits\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-6\", \"name\":\"Fruit Baskets\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1D\", \"name\":\"Exotic Fruits & Veggies\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1D-1\", \"name\":\"Exotic Fruits\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1D-2\", \"name\":\"Exotic Vegetables\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1E\", \"name\":\"Organic Fruits & Vegetables\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1E-1\", \"name\":\"Organic Vegetables\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1E-2\", \"name\":\"Organic Fruits\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1F\", \"name\":\"Cuts & Sprouts\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1F-1\", \"name\":\"Cut & Peeled Veggies\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1F-2\", \"name\":\"Cut Fruits, Tender Coconut\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1F-3\", \"name\":\"Fresh Salads & Sprouts\", \"iKey\":\"Blobkey\"} ] } ]\" } ]}'''

# 1. 解析外层JSON
outer_data = json.loads(raw_json)

# 2. 提取value字段的字符串值
value_str = outer_data['vendorcategory'][0]['value']

# 3. 二次解析字符串,得到真正的数组
try:
    inner_data = json.loads(value_str)
    # 测试:打印第一个子分类名称
    print(inner_data[0]['name'])  # 输出:Fresh Vegetables
except json.JSONDecodeError as e:
    print(f"解析内部JSON失败:{e}")

JavaScript 示例(浏览器/Node.js通用)

// 你的目标JSON字符串
const rawJson = `{ "vendorcategory": [ { "id": "ctg-1", "_cO": 1598431685530, "_dL": 1, "name": "Fruits & Vegetables", "value": "[ { \"id\":\"ctg-1A\", \"name\":\"Fresh Vegetables\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1A-1\", \"name\":\"Potato, Onion, Tomato\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-2\", \"name\":\"Leafy Vegetables\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-3\", \"name\":\"Root Vegetables\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-4\", \"name\":\"Cucumber & Capsicum\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-4\", \"name\":\"Cabbage & Cauliflower\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-5\", \"name\":\"Beans, Brinjals & Okra\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-6\", \"name\":\"Gourd, Pumpkin, Drumstick\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1A-7\", \"name\":\"Specialty\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1B\", \"name\":\"Herbs & Seasonings\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1B-1\", \"name\":\"Lemon, Ginger & Garlic\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1B-2\", \"name\":\"Indian & Exotic Herbs\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1C\", \"name\":\"Fresh Fruits\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1C-1\", \"name\":\"Mangoes\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-2\", \"name\":\"Bananna, Sopota & Papaya\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-3\", \"name\":\"Apples & Pomegranate\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-4\", \"name\":\"Kivi, Melon, Citrus fruit\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-5\", \"name\":\"Seasonal Fruits\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1C-6\", \"name\":\"Fruit Baskets\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1D\", \"name\":\"Exotic Fruits & Veggies\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1D-1\", \"name\":\"Exotic Fruits\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1D-2\", \"name\":\"Exotic Vegetables\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1E\", \"name\":\"Organic Fruits & Vegetables\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1E-1\", \"name\":\"Organic Vegetables\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1E-2\", \"name\":\"Organic Fruits\", \"iKey\":\"Blobkey\"} ] }, { \"id\":\"ctg-1F\", \"name\":\"Cuts & Sprouts\", \"iKey\":\"Blobkey\", \"value\":[ {\"id\":\"ctg-1F-1\", \"name\":\"Cut & Peeled Veggies\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1F-2\", \"name\":\"Cut Fruits, Tender Coconut\", \"iKey\":\"Blobkey\"}, {\"id\":\"ctg-1F-3\", \"name\":\"Fresh Salads & Sprouts\", \"iKey\":\"Blobkey\"} ] } ]\" } ]}`;

// 1. 解析外层JSON
const outerData = JSON.parse(rawJson);

// 2. 提取value字段的字符串值
const valueStr = outerData.vendorcategory[0].value;

// 3. 二次解析字符串
try {
    const innerData = JSON.parse(valueStr);
    // 测试:打印第一个子分类名称
    console.log(innerData[0].name); // 输出:Fresh Vegetables
} catch (err) {
    console.error('解析内部JSON失败:', err);
}

注意事项

  • 一定要确保内层的字符串是合法的JSON格式,如果有转义错误或者语法问题,二次解析会报错,所以建议加上异常处理逻辑,避免程序崩溃。
  • 如果你的JSON数据来自API接口,最好和后端确认一下为什么要把数组序列化存成字符串——正常情况下应该直接嵌套数组,这样前端解析更方便。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 13:57:31