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

Android Studio中如何从JSON字符串值中拆分出名称与描述信息

Split API Response String into Separate Name and Description Fields

Got it, let's tackle this problem. Your goal is to split that combined string into distinct name and description fields for cleaner data handling—totally a smart move for organized data classes. Here's a practical, flexible approach to make this work:

Core Idea: Leverage the Separator Pattern

Looking at your example string, there's a clear - (space-hyphen-space) separator between the character name and their description. We can use this pattern to split the string reliably, even if the description later contains hyphens.

Example Implementation (Python)

Here's a straightforward function that takes the combined text string and returns structured data matching your desired format:

def split_name_description(text):
    # Split only once at the first " - " separator to avoid breaking the description
    name_part, *description_part = text.split(" - ", 1)
    # Handle cases where the separator might be missing in some responses
    description = " - ".join(description_part).strip() if description_part else ""
    return {
        "name": name_part.strip(),
        "description": description
    }

How This Works

  • split(" - ", 1) ensures we only split the string once, so any hyphens in the description won't interfere with the name extraction.
  • .strip() removes extra leading/trailing whitespace from both fields for clean data.
  • The fallback logic handles edge cases where the separator isn't present (so your code won't throw errors if some API responses are formatted differently).

Integrate with Your Data Class

Once you have this function, you can use it to parse API responses directly into your existing data class. For example, if you're using Python dataclasses:

from dataclasses import dataclass

@dataclass
class Character:
    name: str
    description: str

# Sample API response data
api_response = {
    "Text": "Alma Gutierrez - Alma M. Gutierrez is a fictional character on the HBO drama The Wire, played by actress Michelle Paress. Gutierrez is a dedicated and idealistic young reporter on the city desk of The Baltimore Sun."
}

# Parse and map to your data class
split_data = split_name_description(api_response["Text"])
character = Character(**split_data)

print(character.name)  # Output: Alma Gutierrez
print(character.description)  # Output: Alma M. Gutierrez is a fictional character...

Edge Case Adjustments

If you encounter variations in the API response format, you can tweak the function easily:

  • If the separator is just - without spaces, change the split string to "-" (but test with your actual data first).
  • If some entries have no description at all, the function will return an empty string for description instead of causing errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 18:28:13