3

I would like to extract the values from JSONArray. JSONArray has N number of rows and columns.

        ObjectMapper mapper = new ObjectMapper();
        DynamicForm dynamicForm = new DynamicForm();
        dynamicForm = dynamicForm.bindFromRequest();
        Dynamic dynamic = dynamicForm.get(); 
        //List<OneModel> list = new ArrayList<OneModel>();
        //List iterate=new ArrayList();

        String data = dynamic.getData().get("content").toString();
        try {
            JSONArray jsonArray = new JSONArray(data);
            for (int i = 0; i < jsonArray.length(); i++) {
                System.out.println(jsonArray.get(i));
        } }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

Its resulting as follows.

["1001432","05-KALENJI-P1229","KALENJI","2","2014-11-09 09:37:14.379482",""],
["1001432","05-KALENJI-P1228","KALENJI","1","2014-11-09 09:37:14.379482",""],
["1001432","05-KALENJI-P1227","KALENJI","5","2014-11-09 09:37:14.379482",""]

I would like to extract one by one values and assign it to variable. for example 1001432,05-KALENJI-P1229,KALENJI,2,2014-11-09 09:37:14.379482. So that i can process each values. Please any one help me in the same

5
  • 1
    Please post the Json which you wanna to retrive Commented Nov 20, 2014 at 9:19
  • [["1001556","5-KIPSTA","KIPSTA","1","2014-11-09 15:12:44.713654"]}] Commented Nov 20, 2014 at 9:53
  • @KathirvelAppusamy That's not valid JSON. Please post the full value of the content key, i.e. the value of dynamic.getData().get("content").toString() Commented Nov 20, 2014 at 10:04
  • The following is the array which i am getting from the dynamic.getData().get("content").toString() [["1001556","5-KIPSTA-P414","KIPSTA","1","2014-11-09 15:12:44.713654"],["1001556","5-KIPSTA-P412","KIPSTA","1","2014-11-09 15:12:44.713654"]] Commented Nov 20, 2014 at 10:11
  • Can you please let me know how to parse the same. Commented Nov 20, 2014 at 10:12

3 Answers 3

9

You can use the following code:

//put your json in the string variable "data"

JSONArray jsonArray=new JSONArray(data);
            if(jsonArray!=null && jsonArray.length()>0){
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONArray childJsonArray=jsonArray.optJSONArray(i);
                    if(childJsonArray!=null && childJsonArray.length()>0){
                        for (int j = 0; j < childJsonArray.length(); j++) {
                            System.out.println(childJsonArray.optString(j));
                        }
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

6 Comments

It would be great if you can show me how to extract index by index(in the do your code here part)
Please share your full json, so i will extract it and share perfact code .
here is my complete jsonarray [["1001556","5-KIPSTA-P414","KIPSTA","1","2014-11-09 15:12:44.713654"],["1001556","5-KIPSTA-P412","KIPSTA","1","2014-11-09 15:12:44.713654"]]. Please help me
@KathirvelAppusamy Please check code, it's work fine, and please accept answer.
@Krunal Indrodiya I think it's not a good behavior to fource the op to accept your answer.
|
1

It looks like the JSON array is 2 dimensional. Try this:

JSONArray outerArray = new JSONArray(data);
for (int i = 0; i < outerArray.length(); i++) {
    JSONArray innerArray = outerArray.getJSONArray(i);

    for (int j = 0; j < outerArray.length(); j++) {
        System.out.println(innerArray.get(j));
    }
}

Comments

0

Your can iterate over the array via a loop and get your objects like this:

 JsonObject home = array.getJsonObject(index); //use index to iterate
 String num = home.getString("number"); //make number a static final variable

The "number" is the name for the value, which was originally put in with

.add("number", someString);

Greets.

edit: I recommend to read the docu: Oracle Doc and this, too.

2 Comments

There is no index called number. I can iterate through only index values such as 0,1,2,3 etc.. So Please give me some other solution. Its not working for me
If you don't know the label of the strings, then the optString method provided by some of the other answers should do the job, as well.

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.