0

很长一段时间以来,我一直在尝试将包含数组的字符串加载到 JSONArray 中。我从包含电影数组的网络服务中获取以下字符串:{“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 回答