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

如何以.", 为分隔符拆分API返回JSON中steps字段字符串为Array?

Hey there, let's tackle this problem step by step. First, I notice the steps field in your API response is a string that represents a JSON array (wrapped in [] with quoted elements), not just a plain text string. Here are solid implementation approaches using JavaScript and Python—two of the most common languages for handling API data:


JavaScript Implementation (Frontend/Node.js)

Assuming you've already fetched and parsed the full API response into a JavaScript object, here's how to handle the steps field:

// Your parsed API response object
const apiResponse = {"message": "success", "data": [{"id": 1, /* rest of your data */}]};

// Grab the steps string from the response
const stepsString = apiResponse.data[0].steps;

// Option 1: Split by `", "` as you requested (with cleanup)
// First strip the outer [] brackets, then split, then remove leftover quotes from each step
const stepsArrayManual = stepsString
  .slice(1, -1) // Chop off leading [ and trailing ]
  .split('", "') // Split on the `", "` separator
  .map(step => step.replace(/^"|"$/g, '')); // Remove remaining quotes from each step

// Option 2: Proper JSON parsing (WAY more reliable—avoid manual string hacks!)
// This handles all escape characters correctly, which manual splitting can miss
const stepsArrayParsed = JSON.parse(stepsString);

// If you wanted to split each individual step into smaller chunks by `. `, do this after parsing:
const splitIndividualSteps = stepsArrayParsed.map(step => step.split('. '));

Python Implementation (Backend/Scripts)

For Python workflows, use the built-in json module for clean handling:

import json

# Your parsed API response dictionary
api_response = {"message": "success", "data": [{"id": 1, /* rest of your data */}]}

# Get the steps string from the response
steps_string = api_response['data'][0]['steps']

# Option 1: Manual split (same caveats as above)
steps_array_manual = steps_string[1:-1].split('", "')
# Strip leftover quotes from each step
steps_array_manual = [step.strip('"') for step in steps_array_manual]

# Option 2: Proper JSON parsing (highly recommended)
steps_array_parsed = json.loads(steps_string)

# Split each step by `. ` if needed:
split_individual_steps = [step.split('. ') for step in steps_array_parsed]

Quick Notes

  • Always prefer JSON parsing over manual splitting! If a step ever contains a ", " (unlikely here, but possible in other datasets), manual splitting will break your array. JSON parsing handles all edge cases like escaped characters automatically.
  • If you really meant to split on ".", (dot + quote + comma + space) instead of ", ", just swap out the split string in the manual approach. Looking at your sample data though, the separator between steps is ", ", so I suspect that was a typo.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 06:38:39