如何读取两个连接的JSON文件?

时间:2017-03-23 19:40:17

标签: python json python-3.x

我有以下两个连贯的JSON字符串的文件:

{
  "hello": 2,
  "world": 3
}{
  "something": 5,
  "else": 6
}

它们都是正确的(它们比这更复杂,但总是两个JSON dicts一个接一个)。

由于我可以预测第一个的格式(接近我上面的例子),我将使用正则表达式解析文件并最终将它们分开(我只需要第二个JSON):

{[\s\S]*?}([.\n]*?)

虽然这个解决方案有效,但我想确保没有更通用的方法来解决这个问题。

3 个答案:

答案 0 :(得分:4)

div { width: 40px; height: 40px; margin: 10px; float: left; border: 2px lightgray solid; padding: 2px; } .inline { float: left; margin: 10px; width: 48px; text-align: center; line-height: 48px; } #row { width: 100%; border: none; padding: 0; margin: 0; height: 60px; } section { width: 40px; height: 40px; margin: 10px; float: left; border: 2px lightgray solid; padding: 2px; } nav { width: 40px; height: 40px; margin: 10px; float: left; border: 2px lightgray solid; padding: 2px; } span { font-size: 14px; word-wrap: break-word; } button { margin: 10px; padding: 10px; background: transparent; border: 2px solid gray; border-radius: 3px; color: gray; font-size: 12px; } button:hover { background: gray; color: white; }将解析一个字符串并返回其对象加上对象序列化结束的索引。只要文档合理地放入内存中,您就可以轻轻地啃掉字符串。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="row">
  <div></div>
  <div class="find"></div>
  <span class="inline">span</span>
  <div><span>find() here</span></div>
  <section></section>
  <nav></nav>
  <div id="start"></div>
</section>
<button class="prev">test prev()</button>
<button class="closest">test closest()</button>
<button class="find">test find()</button>
<button class="reset">reset</button>

答案 1 :(得分:2)

您可以通过将字符串转换为有效的python对象(如字典列表)来简单地格式化字符串,然后使用json模块加载它:

$users = DB::table('users')->where("Active", 0)->get();

答案 2 :(得分:1)

试试这个:

my_str = """{
  "hello": 2,
  "world": 3
}{
  "something": 5,
  "else": 6
}"""

fixed_str = my_str.replace('}{', '},{')

my_json = json.loads("[" + fixed_str + "]")