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};
2
  • 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, 2014 at 15:10
  • possible duplicate of Converting String to int in Java? Nov 26, 2014 at 15:14

2 Answers 2

4

Use Integer.parseInt(String s):

int val = Integer.parseInt(jr1.get(k));
2
  • 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, 2014 at 15:15
  • Why do you start with a List<Integer> if you want an int[]?
    – user1907906
    Nov 26, 2014 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].

2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.