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

JSON文件访问报错:string indices must be integers问题排查与解决

解决读取JSON文件后访问'intents'字段触发TypeError的问题

问题场景

将嵌套JSON数据写入文件后读取,尝试访问'intents'字段时触发错误:TypeError: string indices must be integers

原始JSON结构:

intents = {"intents": [
    {"tag": "greeting",
     "patterns": ["Hi", "Hey", "Is anyone there?", "Hello", "Hay"],
     "responses": ["Hello", "Hi", "Hi there"]
    },
    {"tag": "goodbye",
     "patterns": ["Bye", "See you later", "Goodbye"],
     "responses": ["See you later", "Have a nice day", "Bye! Come back again"]
    },
    {"tag": "thanks",
     "patterns": ["Thanks", "Thank you", "That's helpful", "Thanks for the help"],
     "responses": ["Happy to help!", "Any time!", "My pleasure", "You're most welcome!"]
    },
    {"tag": "about",
     "patterns": ["Who are you?", "What are you?", "Who you are?" ],
     "responses": ["I.m Joana, your bot assistant", "I'm Joana, an Artificial Intelligent bot"]
    },
    {"tag": "name",
    "patterns": ["what is your name", "what should I call you", "whats your name?"],
    "responses": ["You can call me Joana.", "I'm Joana!", "Just call me as Joana"]
    },
    {"tag": "help",
    "patterns": ["Could you help me?", "give me a hand please", "Can you help?", "What can you do for me?", "I need a support", "I need a help", "support me please"],
    "responses": ["Tell me how can assist you", "Tell me your problem to assist you", "Yes Sure, How can I support you"]
    },
    {"tag": "createaccount",
    "patterns": ["I need to create a new account", "how to open a new account", "I want to create an account", "can you create an account for me", "how to open a new account"],
    "responses": ["You can just easily create a new account from our web site", "Just go to our web site and follow the guidelines to create a new account"]
    },
    {"tag": "complaint",
    "patterns": ["have a complaint", "I want to raise a complaint", "there is a complaint about a service"],
    "responses": ["Please provide us your complaint in order to assist you", "Please mention your complaint, we will reach you and sorry for any inconvenience caused"]
    }
]
}

用户尝试的代码:

new_intents = json.dumps(intents, indent=4)
with open('json_data.json', 'w') as outfile:
    json.dump(new_intents, outfile)
with open('json_data.json') as json_file:
    data = json.load(json_file)
print(data['intents'])

错误原因

写入文件的步骤犯了重复序列化的错误:

  1. json.dumps(intents, indent=4)将Python字典转换成了JSON格式的字符串
  2. json.dump(new_intents, outfile)又把这个字符串作为对象写入文件(json.dump的作用是将Python对象直接序列化为JSON写入文件,不需要提前转成字符串)
  3. 读取时json.load(json_file)得到的是完整的JSON字符串,而非解析后的字典。此时用data['intents']访问自然报错——字符串仅支持整数索引,不支持键名索引。

修复方案

方案1:直接写入原始字典(推荐)

跳过json.dumps步骤,直接将Python字典传入json.dump,它会自动完成序列化并格式化输出:

with open('json_data.json', 'w') as outfile:
    json.dump(intents, outfile, indent=4)
with open('json_data.json') as json_file:
    data = json.load(json_file)
print(data['intents'])

方案2:若需保留json.dumps步骤

如果一定要先转成字符串,写入时直接用文件对象的write方法,读取时先读取字符串再解析:

new_intents = json.dumps(intents, indent=4)
with open('json_data.json', 'w') as outfile:
    outfile.write(new_intents)
with open('json_data.json') as json_file:
    data_str = json_file.read()
    data = json.loads(data_str)
print(data['intents'])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 14:15:40