如何解析JSON对象的Android工作室 [英] How to parse JSON Object Android Studio
本文介绍了如何解析JSON对象的Android工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JSON对象
{标题:九二,额定:PG-13,释放:年,蝙蝠侠归来九二6月19日 运行时:126分,类型:行动,导演:蒂姆·波顿,作家:鲍勃·凯恩(蝙蝠侠字符),丹尼尔·沃特斯(故事),萨姆·哈姆(故事),丹尼尔·沃特斯(剧本),演员:迈克尔·基顿,丹尼·德维托,米歇尔·菲佛,克里斯托弗·沃肯,语言:英语,国家:美国,英国,大奖:提名2奥斯卡另2胜&安培; 15 nominations.\",\"Poster\":\"http://ia.media-imdb.com/images/M/MV5BODM2OTc0Njg2OF5BMl5BanBnXkFtZTgwMDA4NjQxMTE@._V1_SX300.jpg\",\"Metascore\":\"N/A\",\"imdbRating\":\"7.0\",\"imdbVotes\":\"199,878\",\"imdbID\":\"tt0103776\",\"Type\":\"movie\",\"Response\":\"True\"}
我试图解析在Android Studio中这个对象然而即时得到一个错误:
类型org.json.JSONObject不能转换成JSONArray
这是code,我使用
JSONArray mJsonArray =新JSONArray(jsonResult);
的JSONObject movieObject = mJsonArray.getJSONObject(0);字符串title = movieObject.getString(标题);
解决方案
您JSON包含一个对象,而不是一个数组。替换
JSONArray mJsonArray =新JSONArray(jsonResult);
按
的JSONObject movieObject =新的JSONObject(jsonResult);
字符串title = movieObject.getString(标题);
The JSON Object
{"Title":"Batman Returns","Year":"1992","Rated":"PG-13","Released":"19 Jun 1992","Runtime":"126 min","Genre":"Action","Director":"Tim Burton","Writer":"Bob Kane (Batman characters), Daniel Waters (story), Sam Hamm (story), Daniel Waters (screenplay)","Actors":"Michael Keaton, Danny DeVito, Michelle Pfeiffer, Christopher Walken","Language":"English","Country":"USA, UK","Awards":"Nominated for 2 Oscars. Another 2 wins & 15 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BODM2OTc0Njg2OF5BMl5BanBnXkFtZTgwMDA4NjQxMTE@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"7.0","imdbVotes":"199,878","imdbID":"tt0103776","Type":"movie","Response":"True"}
I'm trying to parse this object in android studio however im getting an error:
of type org.json.JSONObject cannot be converted to JSONArray
This is the code that I'm using
JSONArray mJsonArray = new JSONArray(jsonResult);
JSONObject movieObject = mJsonArray.getJSONObject(0);
String title = movieObject.getString("Title");
解决方案
Your json contains an object, not an array. Replace
JSONArray mJsonArray = new JSONArray(jsonResult);
by
JSONObject movieObject = new JSONObject(jsonResult);
String title = movieObject.getString("Title");
这篇关于如何解析JSON对象的Android工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文