0

I am parsing a JSON as shown below

[
    {
        "vendor_itms_arr": [
            "265",
            "141",
            "148"
        ]
    }
]

This is my program

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MrTest {

    public static void main(String args[]) throws JSONException
    {
        String json = "[\r\n" + 
                "    {\r\n" + 
                "        \"vendor_itms_arr\": [\r\n" + 
                "            \"265\",\r\n" + 
                "            \"141\",\r\n" + 
                "            \"148\"\r\n" + 
                "        ]\r\n" + 
                "    }\r\n" + 
                "]";

        JSONArray array = new JSONArray(json);
        ArrayList<Integer> vendor_itms_arr = new ArrayList<Integer>();
        for(int i=0;i<array.length();i++)
        {
             JSONObject jb1 = array.getJSONObject(i);
             JSONArray jr1 = jb1.getJSONArray("vendor_itms_arr");
            if (jr1 != null) { 
                   int len = jr1.length();
                   for (int k=0;i<len;i++){ 
                       int val = (Integer) jr1.get(k);
                       vendor_itms_arr.add(val);
                   } 
                } 
        }
        System.out.println(vendor_itms_arr);
    }
}

I am getting

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at MrTest.main(MrTest.java:36)

Could you please let me know how can i construct a array

 int constructed_array[] = { 265 , 141 , 148};
ToYonos
  • 14,576
  • 2
  • 40
  • 62
Pawan
  • 28,159
  • 84
  • 232
  • 394
  • 2
    `["265"]` is a json-encoded array containing a string. `[265]` is an array containing an integer. Your json contains only strings. – Marc B Nov 26 '14 at 15:10
  • possible duplicate of [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – Thomas Weller Nov 26 '14 at 15:14

2 Answers2

4

Use Integer.parseInt(String s):

int val = Integer.parseInt(jr1.get(k));
  • Thank you , how to convert this to primitive int array , i have tried as int a[] = vendor_itms_arr.toArray(new int[vendor_itms_arr.size()]); but its not working . – Pawan Nov 26 '14 at 15:15
  • Why do you start with a `List` if you want an `int[]`? –  Nov 26 '14 at 15:34
1

Change your program from Integer to String and try this :

String constructed_array[] = { "265", "141", "148" };
List<Integer> myNewList = new ArrayList<Integer>(constructed_array.length);
for (String str : constructed_array) {
    myNewList.add(Integer.parseInt(str));
}

Now the object myNewList contains [265, 141, 148].

Edit

For the primitive array you can do this :

String constructed_array[] = { "265", "141", "148" };
int[] myPrimitiveArray = new int[constructed_array.length];
for (int i = 0; i < constructed_array.length; i++) {
    myPrimitiveArray[i] = Integer.parseInt(constructed_array[i]);
}

myPrimitiveArray is a primitive array that contain [265, 141, 148].

Anarki
  • 353
  • 4
  • 19
  • Thank you very much , how to convert this to primitive array ?? – Pawan Nov 26 '14 at 15:21
  • @PreethiJain: that would be duplicate to http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java Could you please do some research before asking questions? – Thomas Weller Nov 26 '14 at 15:23