猿问

Python 以不同的方式解析 JSON 对象

我正在开发一个 python 项目,我们正在从用户那里获得一些输入。我们实际上正在研究微服务部署。用户需要提供以下内容:


1):用户将提供一个 GitHub 存储库,其中包含他想要部署在特定目录中的所有微服务。例如,我们在 GitHub 存储库中有这样的目录结构: mysvcs

 |----nodeservice

 |----pyservice


2):用户将提供一个 JSON 对象,他将在其中提及此 repo 的 URL 以及这些微服务的一些其他信息,如下所示:


{

  "repo_url": "https://github.com/arycloud/mysvcs.git",


  "services":[

        {

        "name": "pyservice",

        "routing": {

          "path": "/",

          "service": "pyservice",

          "port": "5000"

        }

      },

      {

        "name": "nodeservice",

        "routing": {

          "path": "/",

          "service": "nodeservice",

          "port": "8080"

        }

      }

        ]

}

然后我们从 GitHub 存储库中读取所有服务并使用它们的目录来读取源代码。与此同时,我们正在解析 JSON 对象以获取有关这些服务的一些信息。


我们正在阅读这样的回购:


tempdir = tempfile.mkdtemp()

saved_unmask = os.umask(0o077)

out_dir = os.path.join(tempdir)

Repo.clone_from(data['repo_url'], out_dir)

list_dir = os.listdir(out_dir)

print(list_dir)

services = []

for svc in range(0, len(data['services'])):

    services.append(list_dir[svc])


print(services)

根据我们上面的例子,它会返回:


['nodesvc', 'pyservice']

但是当我们读取 JSON 对象时,用户以不同的顺序而不是按字母顺序提到了服务,所以当我们使用上述数组循环服务时,我们试图对 JSON 对象服务和列表使用相同的索引克隆 GitHub 存储库后的目录,但由于顺序不同,它会交换数据。


这是一个示例代码:


def my_deployment(data):

    # data is JSON object

    # Clone github repo and grab Dockerfiles

    tempdir = tempfile.mkdtemp()

    saved_unmask = os.umask(0o077)

    out_dir = os.path.join(tempdir)

    Repo.clone_from(data['repo_url'], out_dir)

    list_dir = os.listdir(out_dir)

    print(list_dir)

    services = []

    for svc in range(0, len(data['services'])):

        services.append(list_dir[svc])

    print(services)

    for service in range(len(services)):

        # Here i need to use the data from JSON object for current service 

        data['services'][service]['routing']['port']

        # Here it's using the data of **pyservice** instead of **nodeservice** and vice versa.

重要提示:GitHub 中的服务顺序是,['nodeservices', 'nodeservices']但在 JSON 对象中,用户可以以不同的顺序提及他的服务,例如pyservices, nodeservices. 那么当我们循环时,我们如何同步这两个源的顺序?这是主要问题。


jeck猫
浏览 202回答 3
3回答

牛魔王的故事

你想的太复杂了。for svc in data['services']:    print(svc['name'], svc['routing']['port'])完毕。一般观察:您似乎坚持循环索引。别。Python 循环没有索引是一件好事。每当你想写作时for thing in range(len(some_list)):停下来写for thing in some_list:反而。

慕村9548890

这是我们可以用来克服这个订单同步问题的方法:首先,GitHub 存储库中的目录顺序默认是按字母顺序排列的,因此如果我们对 JSON 对象中的服务数组进行排序,我们将能够为两个源获得相同的索引。甚至确保我们可以按字母顺序对这两个来源进行排序。这是代码:首先将 JSON 对象的 services 数组排序为:data['services'] = sorted(data["services"], key=lambda d: d["name"])通过考虑问题中的示例,它将为我们提供:services = [   {"nodeservice": {     "A": "B"     }   },   {"pyservice":{     "X": "Y"    }   }]然后我们将对 GitHub 存储库中的目录列表进行排序,如下所示:Repo.clone_from(data['repo_url'], out_dir)list_dir = os.listdir(out_dir)print(list_dir)services = []for svc in range(0, len(data['services'])):    services.append(list_dir[svc])services.sort()print(services)它会给我们:['nodeservice', 'pyservice']根据上面问题中的例子。因此,在这两种情况下,我们都有第nodeservice一个pyservice, 表示相同的顺序。
随时随地看视频慕课网APP

相关分类

Python
我要回答