将JSON转换为String

时间:2017-02-07 19:11:08

标签: json android-studio

我遇到了将JSON数据转换为可读文本的问题。现在它出现了这样的事情:

{"id":82,"url":"http://www.tvmaze.com/shows/82/game-of-thrones","name":"Game of Thrones","type":"Scripted","language":"English","genres":["Drama","Adventure","Fantasy"],"status":"Running","runtime":60,"premiered":"2011-04-17","schedule":{"time":"21:00","days":["Sunday"]},"rating":{"average":9.3},"weight":10,"network":{"id":8,"name":"HBO","country":{"name":"United States","code":"US","timezone":"America/New_York"}},"webChannel":null,"externals":{"tvrage":24493,"thetvdb":121361,"imdb":"tt0944947"},"image":{"medium":"http://static.tvmaze.com/uploads/images/medium_portrait/53/132622.jpg","original":"http://static.tvmaze.com/uploads/images/original_untouched/53/132622.jpg"},"summary":"<p>Based on the bestselling book series <em>A Song of Ice and Fire</em> by George R.R. Martin, this sprawling new HBO drama is set in a world where summers span decades and winters can last a lifetime. From the scheming south and the savage eastern lands, to the frozen north and ancient Wall that protects the realm from the mysterious darkness beyond, the powerful families of the Seven Kingdoms are locked in a battle for the Iron Throne. This is a story of duplicity and treachery, nobility and honor, conquest and triumph. In the <em>\"Game of Thrones\"</em>, you either win or you die.</p>","updated":1485102249,"_links":{"self":{"href":"http://api.tvmaze.com/shows/82"},"previousepisode":{"href":"http://api.tvmaze.com/episodes/729575"},"nextepisode":{"href":"http://api.tvmaze.com/episodes/937256"}}}

我如何转换这些数据,以便将其视为ID,Name,Genre等?

1 个答案:

答案 0 :(得分:0)

如果您只想将其打印到控制台,请尝试以下方法:

    var json = '{"id":82,"url":"http://www.tvmaze.com/shows/82/game-of-thrones","name":"Game of Thrones","type":"Scripted","language":"English","genres":["Drama","Adventure","Fantasy"],"status":"Running","runtime":60,"premiered":"2011-04-17","schedule":{"time":"21:00","days":["Sunday"]},"rating":{"average":9.3},"weight":10,"network":{"id":8,"name":"HBO","country":{"name":"United States","code":"US","timezone":"America/New_York"}},"webChannel":null,"externals":{"tvrage":24493,"thetvdb":121361,"imdb":"tt0944947"},"image":{"medium":"http://static.tvmaze.com/uploads/images/medium_portrait/53/132622.jpg","original":"http://static.tvmaze.com/uploads/images/original_untouched/53/132622.jpg"},"summary":"<p>Based on the bestselling book series <em>A Song of Ice and Fire</em> by George R.R. Martin, this sprawling new HBO drama is set in a world where summers span decades and winters can last a lifetime. From the scheming south and the savage eastern lands, to the frozen north and ancient Wall that protects the realm from the mysterious darkness beyond, the powerful families of the Seven Kingdoms are locked in a battle for the Iron Throne. This is a story of duplicity and treachery, nobility and honor, conquest and triumph. In the <em>Game of Thrones</em>, you either win or you die.</p>","updated":1485102249,"_links":{"self":{"href":"http://api.tvmaze.com/shows/82"},"previousepisode":{"href":"http://api.tvmaze.com/episodes/729575"},"nextepisode":{"href":"http://api.tvmaze.com/episodes/937256"}}}'

    var  obj = jQuery.parseJSON(json) 

    function printLine(obj){

        for( var i in obj){

            if(typeof obj[i] != 'object') console.log(i + ": " +obj[i]);

            else printLine(obj[i]);
        }
    }

    printLine(obj);