0

配列を含む文字列を JSONArray にロードしようとして、かなり長い間いくつかの問題が発生しています。ムービー配列を含む Web サービスから次の文字列を取得します: {"total":3,"movies":[],"links":{......}"}

この配列を JASONArray に変換し、リスト ビューを使用して表示したいと考えています。私は次のコードを使用していますが、機能していません....

        protected void onPostExecute(String result) {

        Log.d(TAG, "result: " + result);

        if (result==null || result.length()==0){
            // no result:
            return;
        }

        //clear the list
        moviesList.clear();

        try {
            //turn the result into a JSON object
            Log.d(TAG, "create responseObject: "); 

            JSONObject responseObject = new JSONObject(result);

            Log.d(TAG, responseObject.toString());

            // get the JSON array named "movies"
            JSONArray resultsArray = responseObject.getJSONArray("movies");             
            Log.d(TAG, "JSONArray lenght: " + resultsArray.length());

            // Iterate over the JSON array: 
            for (int i = 0; i < resultsArray.length(); i++) {
                // the JSON object in position i 
                JSONObject movieObject = resultsArray.getJSONObject(i);
                Log.d(TAG, "movieObject (from array) : " + movieObject.toString());

                // get the primitive values in the object
                String title = movieObject.getString("title");
                String details = movieObject.getString("synopsis");

                //put into the list:
                Movie movie = new Movie(title, details, null,null);
                //public Movie(String title, String details, String urlnet, String urldevice) {
                moviesList.add(movie);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //refresh listView:
        adapter.notifyDataSetChanged();

    }

ここの投稿が問題の解決に役立つことを願っていました。

4

1 に答える 1

0

これを変える:

JSONObject responseObject = new JSONObject(result);

これに:

JSONObject responseObject = (JSONObject) JSONValue.parse(result);

そして、あなたはあなたを得ることができますJSONArray.

于 2013-11-13T10:18:58.680 に答える