【问题标题】:How to parse JSON Object Android Studio如何解析 JSON 对象 Android Studio
【发布时间】:2016-02-17 23:57:02
【问题描述】:

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

我试图在 android studio 中解析这个对象,但是我得到一个错误:

of type org.json.JSONObject cannot be converted to JSONArray

这是我正在使用的代码

JSONArray mJsonArray = new JSONArray(jsonResult);
JSONObject movieObject = mJsonArray.getJSONObject(0);

String title = movieObject.getString("Title");

【问题讨论】:

  • 从日志中很清楚 :JSONObject cannot be converted to JSONArray 表示需要将JSONArray mJsonArray = new JSONArray(jsonResult); 更改为JSONObject mJsonArray = new JSONObject(jsonResult);
  • 要使用键获取值,请执行以下操作:String title = mJsonArray.getString("Title"); 并完全删除 JSONObject movieObject = mJsonArray.getJSONObject(0);

标签: java android json api


【解决方案1】:

您的 json 包含一个对象,而不是一个数组。替换

JSONArray mJsonArray = new JSONArray(jsonResult);

JSONObject movieObject = new JSONObject(jsonResult);    
String title = movieObject.getString("Title");

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2011-07-30
    • 1970-01-01
    • 2023-03-16
    • 2015-10-24
    相关资源
    最近更新 更多