java 如何解析 JSON 对象 Android Studio
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33742944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:04:43 来源:igfitidea点击:
How to parse JSON Object Android Studio
提问by Addil
The JSON Object
JSON 对象
{"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:
我正在尝试在 android studio 中解析这个对象,但是我收到一个错误:
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");
回答by ThomasThiebaud
Your json contains an object, not an array. Replace
您的 json 包含一个对象,而不是一个数组。代替
JSONArray mJsonArray = new JSONArray(jsonResult);
by
经过
JSONObject movieObject = new JSONObject(jsonResult);
String title = movieObject.getString("Title");