4

I'm not able to return a JSONArray, instead my object appears to be a String. the value of myArray is the same value as jsonString. The object is a String object and not a JSONArray. and both jsonString and myArray prnt:

[{"id":"100002930603211",
  "name":"Aardvark Jingleheimer",
  "picture":"shortenedExample.jpg" },
 {"id":"537815695",
   "name":"Aarn Mc",
   "picture":"shortendExample.jpg" },
 {"id":"658471072",
   "name":"Adrna opescu",
   "picture":"shortenedExample.jpg"
}]

How can I convert this to an actual Java JSONArray? thanks!

           //arrPersons is an ArrayList

        Gson gson = new Gson();
        String jsonString = gson.toJson(arrPersons);

        JsonParser parser = new JsonParser();
        JsonElement myElement = parser.parse(jsonString);
        JsonArray myArray = myElement.getAsJsonArray();
6
  • This can't be right. getAsJsonArray() returns a JSONArray. Java is statically typed, what you wrote above would not compile if myArray wasn't really a JsonArray. Commented Apr 4, 2012 at 23:39
  • that's part of my question. the Gson JsonArray object is different than Java JSONArray object. I would like to create a JSONArray. Commented Apr 5, 2012 at 0:28
  • 1
    ? There is no the Java JSONArray object. Do you mean JSONArray from json.org? Commented Apr 5, 2012 at 1:19
  • 1
    And if you are using Gson, why bother with json.org stuff? Commented Apr 5, 2012 at 1:24
  • Hi Max, this is my first Java project so please forgive my ignorance on missing that JSONArray was not a Java Object. I did figure out how to accomplish my task of taking a JSONArray and alphabetizing by "name" element. I posted the code below. The path was JSONArray -- ArrayList--sort list--back to JSONArray through GSON. Would love to hear of a better way though. thanks! Commented Apr 5, 2012 at 17:57

2 Answers 2

1

I think you can do what you want without writing out a json string and then re-reading it:

List<Person> arrPersons = new ArrayList<Person>();

// populate your list

Gson gson = new Gson();
JsonElement element = gson.toJsonTree(arrPersons, new TypeToken<List<Person>>() {}.getType());

if (! element.isJsonArray()) {
// fail appropriately
    throw new SomeException();
}

JsonArray jsonArray = element.getAsJsonArray();
Sign up to request clarification or add additional context in comments.

Comments

0
    public JSONArray getMessage(String response){

    ArrayList<Person> arrPersons = new ArrayList<Person>();
    try {
        // obtain the response
        JSONObject jsonResponse = new JSONObject(response);
        // get the array
        JSONArray persons=jsonResponse.optJSONArray("data");


        // iterate over the array and retrieve single person instances
        for(int i=0;i<persons.length();i++){
            // get person object
            JSONObject person=persons.getJSONObject(i);
            // get picture url
            String picture=person.optString("picture");
            // get id
            String id=person.optString("id");
            // get name
            String name=person.optString("name");

            // construct the object and add it to the arraylist
            Person p=new Person();
            p.picture=picture;
            p.id=id;
            p.name=name;
            arrPersons.add(p);
        }
        //sort Arraylist
        Collections.sort(arrPersons, new PersonSortByName());


    Gson gson = new Gson();
    //gson.toJson(arrPersons);

    String jsonString = gson.toJson(arrPersons);

    sortedjsonArray = new JSONArray(jsonString);



    } catch (JSONException e) {

        e.printStackTrace();
    }

    return sortedjsonArray;

}



public class PersonSortByName implements Comparator<Person>{

    public int compare(Person o1, Person o2) {
    return o1.name.compareTo(o2.name);
    }
    }

  public class Person{
       public String picture;
       public String id;
       public String name;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.