Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - How to parse JSON Object Android Studio

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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...