猿问

如何提取包含在字符串段落之间的 JSON 对象?

我有以下字符串:


...some random text...


{

   "1":"one",

   "2":"two",

   "3":{

      "31":{

         "311":"threeoneone",

         "312":"threeonetwo",

         "313":"threeonethree"

      }

   },

   "4":{

      "41":"fourone",

      "42":"fourtwo",

      "43":"fourthree"

   },

   "5":"five",

   "6":"six"

}


...some more random text...

如何从中提取 JSON?这就是我想要得到的。


{

  "1": "one",

  "2": "two",

  "3": {

    "31": {

      "311": "threeoneone",

      "312": "threeonetwo",

      "313": "threeonethree"

    }

  },

  "4": {

    "41": "fourone",

    "42": "fourtwo",

    "43": "fourthree"

  },

  "5": "five",

  "6": "six"

}

有没有一种 Pythonic 的方式来完成这项工作?


慕姐4208626
浏览 170回答 3
3回答

一只斗牛犬

在没有任何内容假设的情况下在具有混合内容的文件中查找 JSON 对象的更强大的解决方案(非 JSON 内容可能包含不成对的大括号,而 JSON 内容可能包含包含不成对的大括号等的字符串)将是遍历左括号右侧的每次出现{和每次出现的},并尝试将括号之间的子字符串解析为 JSON:import jsonright_indices = [i for i, c in enumerate(s) if c == '}']i = 0while i < len(s) - 1:&nbsp; &nbsp; if s[i] == '{':&nbsp; &nbsp; &nbsp; &nbsp; for j in right_indices:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i < j:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(json.loads(s[i: j + 1]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = j + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except json.decoder.JSONDecodeError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; i += 1给定您在变量中的输入字符串s,输出:{'1': 'one', '2': 'two', '3': {'31': {'311': 'threeoneone', '312': 'threeonetwo', '313': 'threeonethree'}}, '4': {'41': 'fourone', '42': 'fourtwo', '43': 'fourthree'}, '5': 'five', '6': 'six'}

喵喔喔

假设 JSON 没有格式错误,并且假设花括号内的所有内容都是 JSON 对象:jsons = []&nbsp;with open(f) as o:&nbsp; &nbsp; parse_to_json = ""&nbsp;&nbsp; &nbsp; for line in o:&nbsp; &nbsp; &nbsp; &nbsp; if line == "{":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parsing_json_flag = True&nbsp; &nbsp; &nbsp; &nbsp; if parsing_json_flag:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parse_to_json += line&nbsp; &nbsp; &nbsp; &nbsp; if line == "}":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parsing_json_flag = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parse_to_json = ""&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsons.append(parse_to_json)现在,jsons使用您最喜欢的 JSON 解析库转换数组中的所有字符串。

忽然笑

您可以通过识别 json 来使用正则表达式,例如:import reimport jsontext = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacinia efficitur metus, eget finibus leo venenatis non. Sed id massa luctus, hendrerit mauris id, auctor tortor.{&nbsp; &nbsp;"1":"one",&nbsp; &nbsp;"2":"two",&nbsp; &nbsp;"3":{&nbsp; &nbsp; &nbsp; "31":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"311":"threeoneone",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"312":"threeonetwo",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"313":"threeonethree"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;},&nbsp; &nbsp;"4":{&nbsp; &nbsp; &nbsp; "41":"fourone",&nbsp; &nbsp; &nbsp; "42":"fourtwo",&nbsp; &nbsp; &nbsp; "43":"fourthree"&nbsp; &nbsp;},&nbsp; &nbsp;"5":"five",&nbsp; &nbsp;"6":"six"}Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacinia efficitur metus, eget finibus leo venenatis non. Sed id massa luctus, hendrerit mauris id, auctor tortor.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacinia efficitur metus, eget finibus leo venenatis non. Sed id massa luctus, hendrerit mauris id, auctor tortor."""result = re.search(r'[a-zA-Z0-9 ,.\n]+(\{[a-zA-Z0-9 \":\{\},\n]+\})[a-zA-Z0-9 ,.\n]+', text)try:&nbsp; &nbsp; json_string = result.group(1)&nbsp; &nbsp; json_data = json.loads(json_string)&nbsp; &nbsp; print(json_data)except IndexError:&nbsp; &nbsp; print("No json found!")
随时随地看视频慕课网APP

相关分类

Python
我要回答