本文介绍了JSON格式添加\字符,当我追加文件,但不是输出字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下函数从flickr $ c $ \\ c



更新:



您的字符串因为 json.dumps()而被转义,它将对象转换为字符串,之后您可以使用 json.loads(),结果是未转义的。

您可以使用 str()

  cache_file.write(str(CACHE_DICTION))
#{'myparam':'162000,photo:...'

但是它保存到单引号文件的问题,它不是有效的JSON和不兼容 json。 ()



我的建议保持你的代码如上所述,除非你想把它存储到 CACHE_FNAME.json

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


I am using the following function to get json from the flickr API. The string it returns is a properly formatted chunk of 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]

The issue I am having is that when I go to write the json to my cache file it keeps adding in backslashes, like this example:

"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.}

How can I store the JSON to the existing file without these additional \ characters, as it is represented when I print the string?

解决方案

use your_string.decode('string_escape') to unescape \" to "

update:

your string escaped because json.dumps(), it convert object to string and later you can read it using json.loads(), the result are unescaped.

you can save it without slash using str()

cache_file.write(str(CACHE_DICTION))
# {'myparam' :'"162000","photo":...'

but the problem it save to file with single quotes, it not valid json and not compatible with json.loads()

my suggestion keep your code as above, except you want to store it to file CACHE_FNAME.json

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

这篇关于JSON格式添加\字符,当我追加文件,但不是输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:10