1

我有一个看起来像这样的 json:

{
  "course1": [
    {
      "courseName": "test",
      "section": "123",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course2": [
    {
      "courseName": "test",
      "section": "456",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course2": [
    {
      "courseName": "test",
      "section": "789",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course2": [
    {
      "courseName": "test",
      "section": "1011",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course3": [
    {
      "courseName": "test",
      "section": "1213",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course3": [
    {
      "courseName": "test",
      "section": "1415",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ]
}

我想组合任何块/对象/列表(我不知道它叫什么),它们具有相同的键值。像这样:

{
  "course1": [
    {
      "courseName": "test",
      "section": "123",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course2": [
    {
      "courseName": "test",
      "section": "456",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    },
    {
      "courseName": "test",
      "section": "789",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    },
    {
      "courseName": "test",
      "section": "1011",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ],
  "course3": [
    {
      "courseName": "test",
      "section": "1213",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    },
    {
      "courseName": "test",
      "section": "1415",
      "academicHours": "3",
      "day1": "1",
      "room1": "0145 03 1 B 015"
    }
  ]
}

我如何在 python 中使用正则表达式来做到这一点?或任何正则表达式查询?

另外,我尝试json.dumps()从那里使用和工作,但由于某种原因,当我将它与任何包含阿拉伯字符的 json 一起使用时,它会吓坏并弄乱整个事情。所以不幸的是,我坚持使用正则表达式。

并感谢您的帮助:)

4

1 回答 1

3

stdlibjson提供了一个钩子来允许解码具有重复键的对象。这个简单的“扩展”钩子应该适用于您的示例数据:

def myhook(pairs):
    d = {}
    for k, v in pairs:
        if k not in d:
          d[k] = v
        else:
          d[k] += v
    return d

mydata = json.loads(bad_json, object_pairs_hook=myhook)

尽管JSON 规范中没有禁止重复键,但首先应该避免它:

1.1。本文档中使用的约定

本文档中的关键词“必须”、“不得”、“要求”、“应”、“不应”、“应该”、“不应”、“推荐”、“可以”和“可选”是按照 [RFC2119] 中的描述进行解释。

...

  1. 对象

对象结构表示为一对围绕零个或多个名称/值对(或成员)的花括号。名称是一个字符串。每个名称后面都有一个冒号,将名称与值分开。单个逗号将值与后面的名称分开。对象中的名称应该是唯一的。

于 2019-01-25T01:29:53.667 回答