Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been experiencing some problems for quite some time trying to load a string that contains an array into a JSONArray. i get the following string from a web-service which contains the movies array: {"total":3,"movies":[],"links":{......}"}

i'm looking to convert this array into a JASONArray and show it using a list view. i'm using the following code and it not working....

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

    }

I was hoping a post here would help me solve the problem.

share|improve this question
    
"it not working" ... What is not working ? Do you get an error ? If so, post it as well. Thank you. –  2Dee Nov 13 '13 at 10:07
    
can you post more info about your Json object? {"total":3,"movies":[],"links":{......}"} not enough, at least show how movies look like. Thanks –  Maxim Shoustin Nov 13 '13 at 10:09
    
i dont get a JSONArray out of movies from the input string. no error. tx lora –  Lora Nov 13 '13 at 10:09
    
sorry, i'm a rookie in android and here so i can't post more than 2 links ... i've printed to the log the result string i have (Log.d(TAG, "result: " + result);) and the log looks like that: {"total":3,"movies":[],"links":{"self":"api.rottentomatoes.com/api/public/v1.0/…;},"link_template":"http‌​://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit=‌​{results-per-page}&page={page-number}"} –  Lora Nov 13 '13 at 10:14
    
your movies is empty –  Maxim Shoustin Nov 13 '13 at 10:57

1 Answer 1

Change this:

JSONObject responseObject = new JSONObject(result);

to this:

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

and then you can get your JSONArray.

share|improve this answer
    
tx, but what is: JSONValue ? i get the following error: JSONValue cannot be resolved –  Lora Nov 13 '13 at 10:32
    
For working with JSON I use this: link and this library has that field. –  yurezcv Nov 13 '13 at 10:44
    
as a rookie i don't know how to do it.... i've downloaded the library and saved it in my project under the libs, how to include it ? i also tried putting it under com.example.mymovies1 (the package) and : import com.example.mymovies1.*; in both cases i still get the error. i'll appropriate if u can explain it for a rookie ... (: –  Lora Nov 13 '13 at 11:25
    
Go to the Project -> Properties -> Java Build Path -> Libraries -> add external JAR. –  yurezcv Nov 13 '13 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.