3

I am getting following response from an API

String response={"balance":1000,
  "lastliability":2000,"profitarray":[[1,2,3],[67,88,99]],"previous":[99,88]}

I am trying to convert profitarray to ArrayList in Java like

    JSONObject res=new JSONObject(response);
    ArrayList<ArrayList<Integer>> temp=res.getString("profitarray")//something like this
ArrayList<Integer>yy=res.getString("previous");

Obviously, the outputs of res.getstring will be String and I want to convert them to ArrayList, any idea how to do it?

5
  • 1
    Which JSON library are you using and doesn't it already have a way to return Lists/Arrays? Commented Nov 24, 2018 at 9:05
  • Are you asking how to get profitarray":[[1,2,3],[67,88,99]] from the response ? How to get [[1,2,3],[67,88,99]] from the response ? or how to convert this [[1,2,3],[67,88,99]] from a String to arraylist of Integers ? Commented Nov 24, 2018 at 9:05
  • @c0der i amalready getting them from response ,I want to convert them to respective arraylist so as to save them in database Commented Nov 24, 2018 at 9:06
  • @vlaz JSon simple Commented Nov 24, 2018 at 9:06
  • I want to convert the string response in arraylist ,the response is stringified array right. Commented Nov 24, 2018 at 9:11

3 Answers 3

1

Could be a complex way to do it, but since you wanted an ArrayList>, here we are

public static void main(String[] args) {
    String response = "{\"balance\":1000,\n" +
            "  \"lastliability\":2000,\"profitarray\":[[1,2,3],[67,88,99]],\"previous\":[99,88]}";
    JSONObject res=new JSONObject(response);
    JSONArray array = res.getJSONArray("profitarray");
    ArrayList<JSONArray> profitList = new ArrayList<>();
    for (int i = 0; i < array.length(); i++){
        profitList.add(array.getJSONArray(i));
    }
    ArrayList<ArrayList<Integer>> finalList = new ArrayList<>();
    ArrayList<Integer> list1 = new ArrayList<>();
    for (int i = 0; i < profitList.size(); i++){
        JSONArray array1 = profitList.get(i);
        list1 = new ArrayList<>();
        for (int j = 0; j < array1.length(); j++){
            list1.add(array1.getInt(j));
        }
        finalList.add(list1);
    }
}

The ArrayList finalList contains the final output.

Sign up to request clarification or add additional context in comments.

Comments

0

The key thing is to create a Java class that model your response. See this example using Google Gson.

Response class:

import java.util.ArrayList;

public class Response {
//    int balance;
//    int lastLiability;
//    ArrayList<Integer> previous;
    ArrayList<ArrayList<Integer>> profitarray;
}

Gson example:

import com.google.gson.Gson;

public class GsonExample {
    public static void main(String[] args) {
        String json = "{\"balance\":1000,\"lastliability\":2000,\"profitarray\":[[1,2,3],[67,88,99]],\"previous\":[99,88]}";

        Response response = new Gson().fromJson(json, Response.class);
        System.out.println(response.profitarray);
    }
}

I guess that you should add accessors, equals/hashCode and all that noise — but you get the idea.

Comments

0

You can use JsonArray to extract an array out of a JSON response. Later on, you can use Collections API to convert that array into an ArrayList.

    JsonObject res = new JsonObject(response);
    JsonArray array1 = res.getJsonArray("profitarray");
    JsonArray array2 = res.getJsonArray("previous");

    ArrayList<JsonObject> arrayList1 = new ArrayList(array1.length());
    ArrayList<JsonObject> arrayList2 = new ArrayList(array2.length());

    for(int i=0;i < array1.length();i++){
    arrayList1.add(array1.getJsonObject(i));
}
    for(int j=0;j < array2.length();j++){
    arrayList2.add(array2.getJsonObject(j));
}

4 Comments

Is there a documentation for that? All I can find is this but it seems like the API is different - it's using JsonObject instead of JSONObject (different capitalisation). This is version 3, so perhaps it changed - I find some references to code using JSONObject but it seems to be for an earlier version some version 1 project points at a dead documentation source
Thanks for the observation. I have modified my answer for the same. By the by I had referred this: [oracle.com/technetwork/articles/java/json-1973242.html]
It dint work,I need ArrayList<ArrayList<Integer>> :(
array1 would not have worked but array2 would surely do. Isn't it?

Your Answer

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