如何从带单引号的Map格式String中提取指定用户的密码?
提取指定用户的密码
你的原始字符串存在两个问题:首尾带有单引号,内部的引号是HTML转义的"。需要先清理字符串,再解析为JSON结构来提取目标值。
Python 解决方案
import json # 原始输入字符串 raw_str = "'{"userJohn":"1234","userLinda":"9876"}'" # 1. 移除首尾的单引号 cleaned_str = raw_str.strip("'") # 2. 将HTML转义的引号替换为普通双引号 json_str = cleaned_str.replace(""", '"') # 3. 解析JSON为字典 user_map = json.loads(json_str) # 4. 提取userLinda的密码 linda_password = user_map["userLinda"] print(linda_password) # 输出结果:9876
JavaScript 解决方案
// 原始输入字符串 const rawStr = "'{"userJohn":"1234","userLinda":"9876"}'"; // 1. 移除首尾单引号 const cleanedStr = rawStr.slice(1, -1); // 2. 全局替换HTML转义引号 const jsonStr = cleanedStr.replace(/"/g, '"'); // 3. 解析JSON为对象 const userMap = JSON.parse(jsonStr); // 4. 提取目标密码 const lindaPassword = userMap.userLinda; console.log(lindaPassword); // 输出结果:9876
内容的提问来源于stack exchange,提问作者Hard Worker
相关产品推荐
相关产品推荐

