0

I am trying to order my JSON data by name, I've found some answers here, but nothing works for my case. I have tried to update my code as you see below. I marked there what I have removed from original code, and what have I added:

@Override
    protected Boolean doInBackground(String... args) {
        HttpHandler sh = new HttpHandler();
        String url = "androidnews.json";
        String jsonStr = sh.makeServiceCall(url);

        if (jsonStr != null) {
            try {
                //removed this:
                //JSONObject jsonObj = new JSONObject(jsonStr);
                //JSONArray actors = jsonObj.getJSONArray("result");

                //ADDED:
                ArrayList<JSONObject> array = new ArrayList<JSONObject>();
                JSONArray actors = new JSONArray("result");

                for (int i = 0; i < actors.length(); i++) {
                    JSONObject c = actors.getJSONObject(i);

                    //ADDED:
                    array.add(actors.getJSONObject(i));

                    Actors actor = new Actors();

                    actor.setName(c.getString("name"));
                    actor.setThumb(c.getString("thumb"));

                    actorsList.add(actor);
               }

               //ADDED:
               Collections.sort(array, new Comparator<JSONObject>() {

                        @Override
                        public int compare(JSONObject lhs, JSONObject rhs) {
                            // TODO Auto-generated method stub

                            try {
                                return (lhs.getString("name").toLowerCase().compareTo(rhs.getString("name").toLowerCase()));
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                return 0;
                            }
                        }
                    });



           }  catch (final JSONException e) {

                Zoznam.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Zoznam.this.getApplicationContext(),
                                "Data error " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                }); }

            return true;

        } else {

            Zoznam.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Zoznam.this.getApplicationContext(),
                            "Chyba internetového pripojenia.",
                            Toast.LENGTH_LONG).show();
                }
            });
            return false;
        }
    }

But after I test it, I have this error: Value result of type java.lang.String cannot be converted to JSONArray

My JSON begins with: {"result": [{"name": "Joe","thumb": "image.jpg",...

Darksymphony
  • 2,155
  • 30
  • 54

2 Answers2

1

A solution to your current problem:

Im guessing that you are using the org.json library. Currently you are trying to create a JSONArray from the string "result".

This is how you access an array within the JSON file:

JSONObject obj = new JSONObject(Files.readAllLines(Paths.get("/path/to/your/file.json")));
JSONArray arr = obj.getJSONArray("number");

Source: more helpful examples like the one above


Further information:

As it seems like your not to familiar with the org.json approach I would highly recommend taking a look at gson as it provides an easy way to map JSON entries to objects (or even Arrays of an Object).

See: this and this

Gerrit Sedlaczek
  • 1,231
  • 1
  • 14
  • 32
1

Instead of sorting your JSONArray try sorting your Arraylist with custom object and use it. you can do something like this

Collections.sort(actorsList, new Comparator<Actors>() {
    @Override
    public int compare(Actors lhs, Actors rhs) {
        return lhs.getName().compareTo(rhs.getName());
    }
});
karan
  • 8,637
  • 3
  • 41
  • 78