生成JSON文件内容

huangapple go评论95阅读模式
英文:

generate JSON file content

问题

以下是Python代码示例,可生成包含与file-1相同内容的另一个JSON文件,其中每次用户名和优先级不同:

  1. import json
  2. # 从file-1中读取JSON数据
  3. with open('file-1.json', 'r') as infile:
  4. json_data = json.load(infile)
  5. # 创建一个新JSON对象,用于存储重复的数据
  6. new_json_object = {"Aps": []}
  7. # 重复100次
  8. for i in range(1, 101):
  9. # 复制原始数据
  10. new_entry = json_data["Aps"][0].copy()
  11. # 更新用户名和优先级
  12. new_entry["attributes"]["name"] = f"user{i}"
  13. new_entry["attributes"]["priority"] = str(100 - i * 10)
  14. # 添加到新JSON对象中
  15. new_json_object["Aps"].append(new_entry)
  16. # 写入新JSON数据到文件
  17. with open('output.json', 'w') as outfile:
  18. json.dump(new_json_object, outfile, ensure_ascii=False, indent=4)

你可以将原始JSON数据存储在名为file-1.json的文件中,然后运行这段代码以生成包含所需内容的新JSON文件。新文件将被命名为output.json。请确保你的Python环境中已经安装了JSON模块。

英文:

I am a beginner with JSON.

I have the following in my JSON file (file-1) :

  1. {
  2. "Aps": [
  3. {
  4. "type": "opo",
  5. "price_min": 7,
  6. "price_max":10,
  7. "app_time": 0,
  8. "arguments": {
  9. "prices": 15,
  10. "apps_num": "112"
  11. },
  12. "attributes": {
  13. "name":"user1",
  14. "priority":"100"
  15. }
  16. }
  17. }

How write python code that generates another JSON file that contain the same content of file-1 but duplicated 100 time in each time the name of user is different user2, user3 ... user100 and also it's priority.

I have tried the following but it is not working :

  1. for lp in range(100):
  2. with open("sample.json", "w") as outfile:
  3. outfile.write(json_object)

but it is not working ..

the required output is as follow :

  1. {
  2. "Aps": [
  3. {
  4. "type": "opo",
  5. "price_min": 7,
  6. "price_max":10,
  7. "app_time": 0,
  8. "arguments": {
  9. "prices": 15,
  10. "apps_num": "112"
  11. },
  12. "attributes": {
  13. "name":"user1",
  14. "priority":"100"
  15. }
  16. },
  17. {
  18. "type": "opo",
  19. "price_min": 7,
  20. "price_max":10,
  21. "app_time": 0,
  22. "arguments": {
  23. "prices": 15,
  24. "apps_num": "112"
  25. },
  26. "attributes": {
  27. "name":"user2",
  28. "priority":"90"
  29. }
  30. },
  31. {
  32. "type": "opo",
  33. "price_min": 7,
  34. "price_max":10,
  35. "app_time": 0,
  36. "arguments": {
  37. "prices": 15,
  38. "apps_num": "112"
  39. },
  40. "attributes": {
  41. "name":"user2",
  42. "priority":"80"
  43. }
  44. },
  45. ..............
  46. }

答案1

得分: 2

我用json和copy模块编写了一小段代码。

json用于读取和写入json文件。

copy是因为我在处理引用变量时遇到了一些问题,可以参考copy的文档;如果我更改了'temp'字典,它会影响'file'字典中的所有出现。

  1. import json
  2. import copy
  3. repeats = 100
  4. file = json.loads(open('file.json', 'r').read())
  5. temp1 = json.loads(open('file.json', 'r').read())['Aps'][0]
  6. for repeat in range(repeats):
  7. temp = copy.deepcopy(temp1)
  8. temp['attributes']['name'] = f"user{repeat + 2}"
  9. temp['attributes']['priority'] = f"{repeat*10+100 - repeat*20}"
  10. file['Aps'].append(temp)
  11. temp1 = copy.deepcopy(temp)
  12. json.dump(file, open('file1.json', 'w'), indent=4)
英文:

I made a little code here using json and copy module

json for reading and writing json files

copy because I had some trouble with reference variables see documentation for copy; if I changed 'temp' dict then it would affect all occurrences in 'file' dict

  1. import json
  2. import copy
  3. repeats = 100
  4. file = json.loads(open('file.json', 'r').read())
  5. temp1 = json.loads(open('file.json', 'r').read())['Aps'][0]
  6. for repeat in range(repeats):
  7. temp = copy.deepcopy(temp1)
  8. temp['attributes']['name'] = f"user{repeat + 2}"
  9. temp['attributes']['priority'] = f"{repeat*10+100 - repeat*20}"
  10. file['Aps'].append(temp)
  11. temp1 = copy.deepcopy(temp)
  12. json.dump(file, open('file1.json', 'w'), indent=4)

答案2

得分: 1

你应该首先将你的JSON文件转换为一个Python对象(字典):

  1. import json
  2. file = open('sample.json')
  3. data = json.load(file)
  4. file.close()

现在你可以对你的Aps列表执行操作,比如将第一个对象追加到列表中100次:

  1. for dups in range(100):
  2. data['Aps'].append(data['Aps'][0])

然后,你可以再次将你的字典保存到JSON文件中:

  1. with open("sample.json", "w") as outputfile:
  2. json.dump(data, outputfile)
英文:

You should first convert your json file to a python object (dict):

  1. import json
  2. file = open('sample.json')
  3. data = json.load(file)
  4. file.close()

Now you can do stuff with your Aps list, like appending the first object 100 times to your list.

  1. for dups in range(100):
  2. data['Aps'].append(data['Aps'][0])

Then you save your dict to a json file again:

  1. with open("sample.json", "w") as outputfile:
  2. json.dump(data, outputfile)

huangapple
  • 本文由 发表于 2023年2月19日 19:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定