如何在python中将json转换成纯文本

2024-09-29 19:25:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个python函数,它将从postman调用api

我得到了json格式的结果

[{u'major': 1, u'uuid': u'b5b182c7-eab1-4988-aa99-b5c1517008d8', u'imageurl': u'', u'name': u'Asset 0', u'location': {u'lampid': u'40000c2a691226b3', u'longitude': -121.91999816894531, u'altitude': 33.269363438, u'lampname': u'LAMP_3', u'floorid': u'50522CB5-F8F8-4F43-808A-44CFD86EC0CF', u'suiteid': u'2FF35822-81DB-4469-8AE3-95D34B6EEDB1', u'lastseen': u'2018-08-15T11:10:06.510Z', u'latitude': 37.40650177001953, u'rssi': -39, u'buildingid': u'93CB2950-46D0-4231-B21C-C4691ACFCC1D'}, u'assettypeid': u'07810021-DD92-4BA9-946B-4829AD8E56DC', u'id': u'226854A8-C634-4BB8-9959-0A080B932BD0', u'minor': 5, u'description': None}, {u'locationError': u'no location found', u'major': 1, u'uuid': u'b5b182c7-eab1-4988-aa99-b5c1517008d8', u'imageurl': u'', u'name': u'Asset 3', u'assettypeid': u'07810021-DD92-4BA9-946B-4829AD8E56DC', u'id': u'E6DBA4C2-C2E9-4162-83F4-5906101F8EE8', u'minor': 8, u'description': None}, {u'major': 1, u'uuid': u'b5b182c7-eab1-4988-aa99-b5c1517008d8', u'imageurl': u'', u'name': u'Asset 2', u'location': {u'lampid': u'40000c2a691226b3', u'longitude': -121.91999816894531, u'altitude': 33.269363438, u'lampname': u'LAMP_3', u'floorid': u'50522CB5-F8F8-4F43-808A-44CFD86EC0CF', u'suiteid': u'2FF35822-81DB-4469-8AE3-95D34B6EEDB1', u'lastseen': u'2018-08-15T11:10:12.552Z', u'latitude': 37.40650177001953, u'rssi': -48, u'buildingid': u'93CB2950-46D0-4231-B21C-C4691ACFCC1D'}, u'assettypeid': u'07810021-DD92-4BA9-946B-4829AD8E56DC', u'id': u'8EF8189A-C902-4BCA-B1CA-787FE8E137AA', u'minor': 7, u'description': None}, {u'locationError': u'no location found', u'major': 1, u'uuid

我要在街区

我的代码是

def getAllAssets(api, token):

    url = api + '/v1/assettracking/assets?inline=location'
    headers = {'authorization': 'Bearer ' + token, 'content-type': 'application/json'}

    data = requests.get(url,  headers=headers)
    if data.status_code == requests.codes.ok:
        binary = data.content
        All_Assets = json.loads(binary)
        #print("All_Assets = {0}".format(All_Assets))
        print(All_Assets)

Tags: nameapijsonuuidlocationassetallassets
3条回答
filename = "my_json_file.json"
with open(filename, 'r') as fr:
    pre_ = fr.read()
    lines = pre_.split('\n')
    new_filename = filename.split('.')[0]+".txt" # To keep the same name except ext
    with open(new_filename, "a") as fw:
        fw.write("\n".join(lines))
import json
print json.dumps(All_Assets, sort_keys=True,
    indent=4, separators=(',', ': '))

从文档https://docs.python.org/2/library/json.html

为什么不使用pprint库来打印输出呢。我想它会给你想要的输出

from pprint import pprint
def getAllAssets(api, token):

    url = api + '/v1/assettracking/assets?inline=location'
    headers = {'authorization': 'Bearer ' + token, 'content-type': 'application/json'}

    data = requests.get(url,  headers=headers)
    if data.status_code == requests.codes.ok:
        binary = data.content
        All_Assets = json.loads(binary)
        #print("All_Assets = {0}".format(All_Assets))
        pprint(All_Assets)

相关问题 更多 >

    热门问题