JSON 格式化在我附加文件时添加 \ 字符,但不在输出中添加字符串

JSON formatting adding \ characters when I append file, but not to string in output

我正在使用以下函数从 flickr API 获取 json。字符串 it returns 是 JSON:

格式正确的块
def get_photo_data(photo_id):
    para = {}
    para["photo_id"] = photo_id
    para["method"] = "flickr.photos.getInfo"
    para["format"] = "json"
    para["api_key"] = FLICKR_KEY
    request_data = params_unique_combination("https://api.flickr.com/services/rest/", para)

    if request_data in CACHE_DICTION:
        return CACHE_DICTION[request_data]
    else:
        response = requests.get("https://api.flickr.com/services/rest/", para)
        CACHE_DICTION[request_data] = response.text[14:-1]
        cache_file = open(CACHE_FNAME, 'w')
        cache_file.write(json.dumps(CACHE_DICTION))
        cache_file.close()
        return response.text[14:-1]

我遇到的问题是,当我将 json 写入缓存文件时,它会不断添加反斜杠,如下例所示:

"https://api.flickr.com/services/rest/format-json_method-flickr.photos.getInfo_photo_id-34869402493": "{\"photo\":{\"id\":\"34869402493\",\"secret\":\"56fcf0342c\",\"server\":\"4057\",\"farm\":5,\"dateuploaded\":\"1499030213\",\"isfavorite\":0,\"license\":\"0\",\"safety_level\":\"0\",\"rotation\":0,\"originalsecret\":\"c4d1d316ed\",\"originalformat\":\"jpg\",\"owner\":{\"nsid\":\"150544082@N05\",\"username\":\"ankitrana_\",\"realname\":\"Ankit Rana\",\"location\":\"Cincinnati, USA\",\"iconserver\":\"4236\",\"iconfarm\":5,\"path_alias\":\"ankitrana_\"},\"title\":{\"_content\":\"7\"},\"description\":{\"_content\":\"\"},\"visibility\":{\"ispublic\":1,\"isfriend\":0,\"isfamily\":0},\"dates\":{\"posted\":\"1499030213\",\"taken\":\"2017-06-19 13:43:38\",\"takengranularity\":\"0\",\"takenunknown\":\"0\",\"lastupdate\":\"1499041020\"},\"views\":\"41\",\"editability\":{\"cancomment\":0,\"canaddmeta\":0},\"publiceditability\":{\"cancomment\":1,\"canaddmeta\":0},\"usage\":{\"candownload\":1,\"canblog\":0,\"canprint\":0,\"canshare\":1},\"comments\":{\"_content\":\"0\"},\"notes\":{\"note\":[]},\"people\":{\"haspeople\":0},\"tags\":{\"tag\":[{\"id\":\"150538742-34869402493-5630\",\"author\":\"150544082@N05\",\"authorname\":\"ankitrana_\",\"raw\":\"cincinnati\",\"_content\":\"cincinnati\",\"machine_tag\":0},{\"id\":\"150538742-34869402493-226\",\"author\":\"150544082@N05\",\"authorname\":\"ankitrana_\",\"raw\":\"ohio\",\"_content\":\"ohio\",\"machine_tag\":false}, ... etc., etc.}

如何在没有这些额外的 \ 字符的情况下将 JSON 存储到现有文件中,就像我打印字符串时所表示的那样?

您可以尝试将 python

中的“\”替换为 str.replace 函数

在下一行之后添加代码

cache_file = open(CACHE_FNAME, 'w')

json_item = str(json.dumps(CACHE_DICTION))
json_item.replace("\", "")

并更改此行

cache_file.write(json.dumps(CACHE_DICTION))

cache_file.write(json_item)

让我知道这是否适合你

使用your_string.decode('string_escape')\"转义为"

更新:

你的字符串转义是因为 json.dumps(),它将对象转换为字符串,稍后你可以使用 json.loads() 读取它,结果未转义。

您可以使用 str()

不使用斜线保存它
cache_file.write(str(CACHE_DICTION))
# {'myparam' :'"162000","photo":...'

但是它用单引号保存到文件的问题,它无效 json 并且与 json.loads()

不兼容

我的建议是保留上面的代码,除非你想将它存储到文件 CACHE_FNAME.json

cache_file = open(CACHE_FNAME, 'w')
cache_file.write(response.text)
cache_file.close()
# {"photos":{"page":1,"pages":6478,..}

只需将 \ 替换为空格即可。 我在使用 JSON.

时做了同样的事情
json_new = json.replace('\', '')