简体   繁体   English

将JSON转换为字符串

[英]Converting JSON to String

I am having issues on converting JSON data to readable text. 我在将JSON数据转换为可读文本时遇到问题。 Right now it comes out something like this: 现在,结果如下:

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

How would I go about converting this data so I can view it as ID, Name, Genre etc.? 我将如何转换这些数据,以便可以将其查看为ID,名称,类型等?

If you just want to print it to the console, try something like this: 如果只想将其打印到控制台,请尝试以下操作:

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM