0

I have this JSON data

{"mydata":{"numbers":[12,23,34]}}

By taking JSONObject I get following object:

JSONObject obj = mydataObj.getJSONObject("mydata");

So obj is this:

{"numbers":[12,23,34]}

I need to use the numbers elements. So I will make a for-loop to use elements.

String numbers = myObject.getString("numbers");

This gives a string like this:

["12","23","34"]

How can I convert this to an array or arraylist?
So I will have an array having values of 12, 23 and 34.

Edit: After getting the Array, ArrayList of JSONArray, I will compare "numbers1_Array" and "numbers2_Array". Then I will find the elements that exists in first array but doesn't exists in second array. So selecting data structure type related to which compare operation is easier.

5 Answers 5

3

You could try getJSONArray() instead of getString(), and then simply iterate over the JSONArray and build your ArrayList.

2
  • Is it possible to compare to JSONArrays and find the differences between them ? Commented May 14, 2013 at 13:40
  • I haven't tried it. If you're just interested in finding out whether they're identical or not, you might get away with comparing their String representations, otherwise you might need to iterate. Commented May 14, 2013 at 13:56
1

try this

   JSONArray jArray = jObj.getJSONArray("numbers");
   List list<Long> = new ArrayList<Long>(); 

    for(int i=0; i < jArray .length(); i++)
        list.add(new Long((String)jArray .get(i)));
2
  • What is second array.You have one? Commented May 14, 2013 at 13:43
  • For brevity I removed it. Seconds array's structure is the same as first one. Only values are different. Commented May 14, 2013 at 15:49
0

after getting the object in JSONObject what you have is

JSONObject obj ===>>> {"numbers":[12,23,34]}

now this object is an array so you cant just access an array by passing name only you have to do something like this

JSONArray numbers = (JSONArray) jsonObject.get("numbers");
        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
                    // this iterator.next() is your element from the JSON array
                    //you can store like 
                    // int val = iterator.next()
                    // better you store this in an array 
        }
0

Others solutions are fine, you can also do with your string like this

numbers=numbers.substring(1,s.length()-1).replaceAll("\"","");
List<String> sample = new ArrayList<String>();
sample.addAll(Arrays.asList(s.split(",")));
0

You could do it this way using Guava. https://github.com/google/guava

    String numbers = myObject.getString("numbers");
    List list = new ArrayList();
    JSONArray numbersJsonArray = new JSONArray(numbers);
    for (int i = 0; i < numbersJsonArray.length(); i ++) {
        list.add(numbersJsonArray.getInt(i));
    }// after this loop if what you need is the array list you have it all in the list


    int[] array = Ints.toArray(list); // if you need array of ints

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.