Python中字符串与json间相互转换

一、 字符串转换为Json格式

import json

str = '''
        [{"episode": "", "img": "https://img3.doubanio.com\/view\/photo\/s_ratio_poster\/public\/p2251539632.jpg",
        "title": "火影忍者剧场版:博人传",
        "url": "https:\/\/movie.douban.com\/subject\/26282448\/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88",
        "type": "movie", "year": "2015", "sub_title": "BORUTO -NARUTO THE MOVIE-", "id": "26282448"},
       {"episode": "", "img": "https://img1.doubanio.com\/view\/photo\/s_ratio_poster\/public\/p2259873449.jpg",
        "title": "火影忍者剧场版:终章",
        "url": "https:\/\/movie.douban.com\/subject\/25938113\/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88",
        "type": "movie", "year": "2014", "sub_title": "THE LAST -NARUTO THE MOVIE-", "id": "25938113"}]
    '''
#将字符串转换为json格式
print(type(str))
data = json.loads(str)
print(type(data))
print(data)

 运行结果:

< class 'str'>

< class 'list'>


[{'episode': '', 'img': 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2251539632.jpg',
  'title': '火影忍者剧场版:博人传',
  'url': 'https://movie.douban.com/subject/26282448/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88',
  'type': 'movie', 'year': '2015', 'sub_title': 'BORUTO -NARUTO THE MOVIE-', 'id': '26282448'},
 {'episode': '', 'img': 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2259873449.jpg',
  'title': '火影忍者剧场版:终章',
  'url': 'https://movie.douban.com/subject/25938113/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88',
  'type': 'movie', 'year': '2014', 'sub_title': 'THE LAST -NARUTO THE MOVIE-', 'id': '25938113'}]

 

二、 Json格式转换为字符串 

import json

data = [{'episode': '', 'img': 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2251539632.jpg',
         'title': '火影忍者剧场版:博人传',
         'url': 'https://movie.douban.com/subject/26282448/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88',
         'type': 'movie', 'year': '2015', 'sub_title': 'BORUTO -NARUTO THE MOVIE-', 'id': '26282448'},
        {'episode': '', 'img': 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2259873449.jpg',
         'title': '火影忍者剧场版:终章',
         'url': 'https://movie.douban.com/subject/25938113/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88',
         'type': 'movie', 'year': '2014', 'sub_title': 'THE LAST -NARUTO THE MOVIE-', 'id': '25938113'}]
# 将json格式转换为字符串
print(type(data))
# indent=2按照缩进格式
# ensure_ascii=False可以消除json包含中文的乱码问题
str = json.dumps(data,indent=2,ensure_ascii=False)
print(type(str))
print(str)

 运行结果:



[
  {
    "episode": "",
    "img": "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2251539632.jpg",
    "title": "火影忍者剧场版:博人传",
    "url": "https://movie.douban.com/subject/26282448/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88",
    "type": "movie",
    "year": "2015",
    "sub_title": "BORUTO -NARUTO THE MOVIE-",
    "id": "26282448"
  },
  {
    "episode": "",
    "img": "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2259873449.jpg",
    "title": "火影忍者剧场版:终章",
    "url": "https://movie.douban.com/subject/25938113/?suggest=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85%E5%89%A7%E5%9C%BA%E7%89%88",
    "type": "movie",
    "year": "2014",
    "sub_title": "THE LAST -NARUTO THE MOVIE-",
    "id": "25938113"
  }
]

 

你可能感兴趣的:(基础,Str,Json,Python,乱码)