在新行而不是单行上输出json对象组

在新行而不是单行上输出json对象组,json,python-3.x,pretty-print,Json,Python 3.x,Pretty Print,目前,我的json数据格式为: {"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, "2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, "3": {"name": "camera", "aisle": "M.38", "status": "In Stock"}} 我想重新格式化“块”数据,以便它在线打印每个“组”数据。数据不需要保持j

目前,我的json数据格式为:

{"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, "2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, "3": {"name": "camera", "aisle": "M.38", "status": "In Stock"}}
我想重新格式化“块”数据,以便它在线打印每个“组”数据。数据不需要保持json格式-我只想将信息分解为单独的行(类似于以下内容):

以下是我正在使用的代码:

 result = json.loads(data['searchResults'])['results'][0]
        summary = {
            'name': result['name'],
            'aisle': result['price']['aisle'][0],
            'status': result['inventory']['status'],
            }
        results[store] = summary

with open('Testing.txt', 'w') as outfile:
    outfile.write('\n')
    json.dump(results, outfile)
添加换行符的推荐方法是什么?

如本文所述,您可以使用Python。 希望这有帮助:)

编辑: 抱歉,我应该说得更清楚。 我使用了以下测试代码并实现了您想要的输出:

import json

#example json from your question as text
before_parsed = '{"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, "2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, "3": {"name": "camera", "aisle": "M.38", "status": "In Stock"}}'

#parsing the text to get the results as a json object
results = json.loads(before_parsed)

#print (results)

#############copy the code from here to your program################
#sorting your json output by keys so that you get your output as {"1":{}, "2":{}...}
results_str = json.dumps(results, sort_keys=True)

#removing the outer brackets {}, now the results_str would look something like this "1":{}, "2":{}...
results_str = results_str[1:-1]

#splitting the string by "}," as delimiter, so results_list would look like this ["1":{, "2":{, "3":{}]
results_list = results_str.split("},")

#print the results to the file as per the desired format
with open('Testing.txt', 'w') as outfile:
    outfile.write('\n')
    for p in results_list[:-1]:
        print (p+'}', file=outfile)
    print (results_list[-1], file=outfile)
并将以下内容打印到Testing.txt文件:

"1": {"aisle": "M.53", "name": "camera", "status": "Out of Stock"}
"2": {"aisle": "M.36", "name": "camera", "status": "In Stock"}
"3": {"aisle": "M.38", "name": "camera", "status": "In Stock"}
让我知道,如果它不适合你,或者如果这不是你正在寻找的。 干杯

如本文所述,您可以使用Python。 希望这有帮助:)

编辑: 抱歉,我应该说得更清楚。 我使用了以下测试代码并实现了您想要的输出:

import json

#example json from your question as text
before_parsed = '{"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, "2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, "3": {"name": "camera", "aisle": "M.38", "status": "In Stock"}}'

#parsing the text to get the results as a json object
results = json.loads(before_parsed)

#print (results)

#############copy the code from here to your program################
#sorting your json output by keys so that you get your output as {"1":{}, "2":{}...}
results_str = json.dumps(results, sort_keys=True)

#removing the outer brackets {}, now the results_str would look something like this "1":{}, "2":{}...
results_str = results_str[1:-1]

#splitting the string by "}," as delimiter, so results_list would look like this ["1":{, "2":{, "3":{}]
results_list = results_str.split("},")

#print the results to the file as per the desired format
with open('Testing.txt', 'w') as outfile:
    outfile.write('\n')
    for p in results_list[:-1]:
        print (p+'}', file=outfile)
    print (results_list[-1], file=outfile)
并将以下内容打印到Testing.txt文件:

"1": {"aisle": "M.53", "name": "camera", "status": "Out of Stock"}
"2": {"aisle": "M.36", "name": "camera", "status": "In Stock"}
"3": {"aisle": "M.38", "name": "camera", "status": "In Stock"}
让我知道,如果它不适合你,或者如果这不是你正在寻找的。
干杯

如果格式如此关键,您可能需要自己实现,或者寻找具有更多配置的第三方模块-据我所知,标准库
json
模块只提供了您已经看到的两个选项。我也尝试了这里发布的内容,但对我的代码来说运气不好。如果格式如此关键,您可能需要自己实现它,或者寻找具有更多配置的第三方模块-据我所知,标准库
json
模块只提供了您已经看到的两个选项。我也尝试了这里发布的内容,但我的代码没有太多运气。可能重复,请您在回答周围添加更多上下文。只有链接的答案很难理解,如果网站关闭或不工作,很容易失效。如果你能将你引用的链接/博客中最相关的部分复制到这篇文章中,这将有助于提问者和未来的读者。我已经看过了引用的答案,但仍然不确定如何应用。你能为我提供更多的指导,让我走上正确的方向吗?我可以请你在你的答案周围添加更多的上下文吗。只有链接的答案很难理解,如果网站关闭或不工作,很容易失效。如果你能将你引用的链接/博客中最相关的部分复制到这篇文章中,这将有助于提问者和未来的读者。我已经看过了引用的答案,但仍然不确定如何应用。你能提供更多的指导让我走上正确的方向吗?