PD Caption JSON解析器 节点 功能:解析JSON格式的caption字符串,提取caption和lang字段
| 182 | |
| 183 | |
| 184 | class PD_CaptionJSONParser: |
| 185 | """ |
| 186 | PD Caption JSON解析器 节点 |
| 187 | 功能:解析JSON格式的caption字符串,提取caption和lang字段 |
| 188 | """ |
| 189 | |
| 190 | @classmethod |
| 191 | def INPUT_TYPES(cls): |
| 192 | return { |
| 193 | "required": { |
| 194 | "json_string": ("STRING", { |
| 195 | "default": "", |
| 196 | "multiline": True, |
| 197 | "placeholder": '输入JSON字符串', |
| 198 | "forceInput": True |
| 199 | }), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | RETURN_TYPES = ("STRING", "STRING") |
| 204 | RETURN_NAMES = ("caption", "lang") |
| 205 | FUNCTION = "parse_json" |
| 206 | CATEGORY = "PDuse/Text" |
| 207 | |
| 208 | # 支持列表输入和输出 |
| 209 | INPUT_IS_LIST = {"json_string": True} |
| 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: |
nothing calls this directly
no outgoing calls
no test coverage detected