解析Caption JSON字符串,提取caption和lang 支持批处理:输入列表,输出列表
(self, json_string)
| 210 | OUTPUT_IS_LIST = (True, True) |
| 211 | |
| 212 | def parse_json(self, json_string): |
| 213 | """ |
| 214 | 解析Caption JSON字符串,提取caption和lang |
| 215 | 支持批处理:输入列表,输出列表 |
| 216 | """ |
| 217 | # 确保json_string是列表 |
| 218 | if not isinstance(json_string, list): |
| 219 | json_string = [json_string] |
| 220 | |
| 221 | caption_list = [] |
| 222 | lang_list = [] |
| 223 | |
| 224 | # 处理每个JSON字符串 |
| 225 | for idx, json_str in enumerate(json_string): |
| 226 | try: |
| 227 | # 类型转换 |
| 228 | if isinstance(json_str, list): |
| 229 | json_str = str(json_str[0]) if json_str else "" |
| 230 | if not isinstance(json_str, str): |
| 231 | json_str = str(json_str) |
| 232 | |
| 233 | # 解析JSON |
| 234 | data = json.loads(json_str) |
| 235 | caption = data.get("caption", "") |
| 236 | lang = data.get("lang", "en") |
| 237 | |
| 238 | caption_list.append(caption) |
| 239 | lang_list.append(lang) |
| 240 | |
| 241 | except json.JSONDecodeError as e: |
| 242 | print(f"PD CaptionJSONParser: 第{idx+1}项JSON解析失败: {e}") |
| 243 | # 如果解析失败,将整个字符串作为caption返回 |
| 244 | caption_list.append(json_str.strip()) |
| 245 | lang_list.append("en") |
| 246 | except Exception as e: |
| 247 | print(f"PD CaptionJSONParser: 解析第{idx+1}项时出错: {e}") |
| 248 | caption_list.append(f"解析出错: {str(e)}") |
| 249 | lang_list.append("en") |
| 250 | |
| 251 | print(f"PD CaptionJSONParser: 批量解析完成,共处理 {len(caption_list)} 项") |
| 252 | |
| 253 | return (caption_list, lang_list) |
| 254 | |
| 255 | |
| 256 | # 注册节点 |
nothing calls this directly
no outgoing calls
no test coverage detected