Java 从烂番茄解析JSON数据

Java 从烂番茄解析JSON数据,java,android,json,rotten-tomatoes,Java,Android,Json,Rotten Tomatoes,我正在解析来自的JSON数据。我只是想得到流派数组,并把它放在祝酒辞中。但是没有祝酒词。这是我的密码: public class MovieInfo extends Activity { private static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx"; String[] Genres; @Override protected void onCreate(Bundle savedInstanceSt

我正在解析来自的JSON数据。我只是想得到流派数组,并把它放在祝酒辞中。但是没有祝酒词。这是我的密码:

public class MovieInfo extends Activity
{
    private static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx"; 
    String[] Genres;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movieinfo);

        String MovieID = getIntent().getExtras().getString("MovieID");
        new RequestTask().execute("http://api.rottentomatoes.com/api/public/v1.0/movies/"+MovieID+".json?apikey=" + API_KEY);

    }

    private class RequestTask extends AsyncTask<String, String, String>
    {
        // make a request to the specified url
        @Override
        protected String doInBackground(String... uri)
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response;
            String responseString = null;
            try
            {
                // make a HTTP request
                response = httpclient.execute(new HttpGet(uri[0]));
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() == HttpStatus.SC_OK)
                {
                    // request successful - read the response and close the connection
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);
                    out.close();
                    responseString = out.toString();
                }
                else
                {
                    // request failed - close the connection
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }
            }
            catch (Exception e)
            {
                Log.d("Test", "Couldn't make a successful request!");
            }
            return responseString;
        }
        @Override
        protected void onPostExecute(String response)
        {
            super.onPostExecute(response);

            if (response != null)
            {
                try
                {
                    // convert the String response to a JSON object,
                    // because JSON is the response format Rotten Tomatoes uses
                    JSONObject jsonResponse = new JSONObject(response);

                    // fetch the array of movies in the response
                    JSONArray genres = jsonResponse.getJSONArray("genres");
                    Genres = new String[genres.length()];
                    for (int i = 0; i < genres.length(); i++)
                    {
                        JSONObject movie = genres.getJSONObject(i);
                        Genres[i] = movie.getString("genres");
                    }

                    // add each movie's title to an array
                   for(int i = 0; i < genres.length(); i++)
                   {
                     Toast.makeText(getBaseContext(), ""+Genres[i], Toast.LENGTH_SHORT).show();
                   }
                }
                catch (JSONException e)
                {
                    Log.d("Test", "Failed to parse the JSON response!");
                }
            }
        }
    }
}
我无法解析诸如Title或release之类的对象,这里缺少什么吗?

请尝试以下方法:

// convert the String response to a JSON object,
// because JSON is the response format Rotten Tomatoes uses
JSONObject jsonResponse = new JSONObject(response);

//Array
JSONArray genres = jsonResponse.getJSONArray("genres");

// which gives you this:
// "genres": [
//   "Animation",
//   "Kids & Family",
//   "Science Fiction & Fantasy",
//   "Comedy"
// ]

//Now you can ask length
int howManyGenres = genres.length();
String[] genresStr = new String[howManyGenres];
//You can iterate here
for(int i=0; i<howManyGenres; i++) {
  genresStr[i] = genres.get(i);
}
//将字符串响应转换为JSON对象,
//因为JSON是我们使用的响应格式
JSONObject jsonResponse=新的JSONObject(响应);
//排列
JSONArray genres=jsonResponse.getJSONArray(“genres”);
//这就给了你:
//“流派”:[
//“动画”,
//“儿童与家庭”,
//“科幻与幻想”,
//“喜剧”
// ]
//现在你可以问长度了
int howManyGenres=genres.length();
字符串[]genresStr=新字符串[howManyGenres];
//你可以在这里迭代

对于(int i=0;i是否
Log.d()
调用您的
catch
阻止打印任何内容?@MikeM。请参阅更新。未执行的行是Genres[i]=movie.getString(“Genres”);在此之后,它跳转到catch块并引发异常。java.lang.String类型的0处的值不能转换为JSONObject=>movie.getString(“类型”;--电影有类型?是的,它有一个名为“类型”的数组。你首先关闭
输出
,然后试着从中阅读
“类型”:[06“动画”,07“儿童与家庭”,08“科幻与幻想”,09“喜剧”10],
--这是一个数组。发生错误的原因很明显是,当元素是字符串时,他要求从数组中取出一个对象。(上面的数字是我复制的清单中的行号,而不是JSON的一部分。)@我很抱歉,我刚刚编辑了这个问题,IDK为什么我在问题中读了单词评级而不是体裁。
// convert the String response to a JSON object,
// because JSON is the response format Rotten Tomatoes uses
JSONObject jsonResponse = new JSONObject(response);

//Array
JSONArray genres = jsonResponse.getJSONArray("genres");

// which gives you this:
// "genres": [
//   "Animation",
//   "Kids & Family",
//   "Science Fiction & Fantasy",
//   "Comedy"
// ]

//Now you can ask length
int howManyGenres = genres.length();
String[] genresStr = new String[howManyGenres];
//You can iterate here
for(int i=0; i<howManyGenres; i++) {
  genresStr[i] = genres.get(i);
}