在python中重构JSON

时间:2014-02-04 15:48:52

标签: python json api python-2.7

我从开放电影数据库linked here

中提取JSON输出

在准备某些分析时,我想重新排序Key:value对并​​将此输出写入.txt文件。 API以下列形式提供输出:

{
 "Plot": "A skilled extractor is offered a chance to regain his old life as payment for a task considered to be impossible.", 
 "Rated": "PG-13", 
 "Response": "True", 
 "Language": "English, Japanese, French", 
 "Title": "Inception", 
 "Country": "USA, UK", 
 "Writer": "Christopher Nolan", 
 "Metascore": "74", 
 "imdbRating": "8.8", 
 "Director": "Christopher Nolan", 
 "Released": "16 Jul 2010", 
 "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy", 
 "Year": "2010", 
 "Genre": "Action, Adventure, Mystery", 
 "Awards": "Won 4 Oscars. Another 83 wins & 109 nominations.", 
 "Runtime": "148 min", 
 "Type": "movie", 
 "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg", 
 "imdbVotes": "912,048", 
 "imdbID": "tt1375666"
}

我想对Key:Value对重新排序

Current      Desired structure
    Plot         Title
    Rated        Year
    Response     Rated
    Language     Released
    Title        Runtime
    Country      Genre
    Writer       Director
    Metascore    Writer
    imdbRating   Actors
    Director     Plot
    Released     Language
    Actors       Country
    Year         Awards
    Genre        Poster
    Awards       Metascore
    Runtime      IMDB rating
    Type         IMDB Votes
    Poster       IMDB_ID
    imdbVotes    Type
    imdbID       Response

由于python json.loads()方法将JSON模式转换为dict,是否有一种简单/直观的方法可以在将新模式写入我的文件之前对其进行自定义排序,或者我是否需要编写一个{{ 1}}循环将这些键:值对写入新的字典?

0 个答案:

没有答案