1

I have json data like this:

{ 
    "err_code": "0", 
    "date":"20130121",
    "time_from":"1242", 
    "range":"5",
    "data":[['12313123','BOOK CODE CYFV3M
NUM CODE 3789850802600'],['089898989','BOOK CODE 1F45MN
NUM CODE 3787510241500']] 
}

and I've tried this program:

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

public class JsonKAI {

    public static void main(String[] args) {

        String jStr = "{\"err_code\":\"0\",\"date\":\"20130121\",\"time_from\":\"1242\",\"range\":\"5\",\"data\":[['12313123','BOOK CODE CYFV3M NUM CODE 3789850802600'],['089898989','BOOK CODE 1F45MN NUM CODE 3787510241500']] }";
// Replace this try catch block for all below subsequent examples
        try {
            JSONObject rootObject = new JSONObject(jStr);
            JSONArray rows = rootObject.getJSONArray("data"); // Get all JSONArray data
            int count = rows.length();
            for(int i=0 ; i< count; i++){
                JSONArray jsonArr = rows.getJSONArray(i);
                System.out.println("jsonObject " + i + ": " + jsonArr);
                //for(int j=0 ; j< count; j++){
                //JSONArray jArr = rows.getJSONArray(j);
                //String s = jArr.toString();
                //System.out.println("jsonObject " +s);
            }}
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

The output is:

jsonObject 0: ["12313123","BOOK CODE CYFV3M NUM CODE 3789850802600"]
jsonObject 1: ["089898989","BOOK CODE 1F45MN NUM CODE 3787510241500"]

I wanna ask how to get/parse from ["12313123","BOOK CODE CYFV3M NUM CODE 3789850802600"]

to 12313123 and BOOK CODE CYFV3M NUM CODE 3789850802600 (without '[' and '"')? Please help me. Thank you.

2
  • As a side note, I would recommend using the Gson library instead. Your 2-dimensional json array would be created as a List of Lists. This answer might help if you would like to explore that possibility: stackoverflow.com/a/7925602/877472 Commented Jan 27, 2013 at 2:43
  • You have an array. Extract the elements of the array. Commented Jan 27, 2013 at 3:05

2 Answers 2

1

you could try this:

        String jStr = "{\"err_code\":\"0\",\"date\":\"20130121\",\"time_from\":\"1242\",\"range\":\"5\",\"data\":[['12313123','BOOK CODE CYFV3M NUM CODE 3789850802600'],['089898989','BOOK CODE 1F45MN NUM CODE 3787510241500']] }";
        try {
          JSONObject rootObject = JSONObject.fromObject(jStr);
          JSONArray rows = rootObject.getJSONArray("data"); // Get all JSONArray data
          int count = rows.size();
          for (int i = 0; i < count; i++) {
            JSONArray jsonArr = rows.getJSONArray(i);
            System.out.println("jsonArray " + i + ": " + jsonArr);
            for (Object o : jsonArr) {          
              System.out.println(o);
            }
          }
        } catch (JSONException e) {
        }
Sign up to request clarification or add additional context in comments.

Comments

1

I have the below example:

String str = "{\"items\":[[1,2,3],[4,5,6],[7,8,9]],\"ans\":[[\"1\",\"2\"],[\"Glad\",\"Good\"]]}";
            try{
                JSONObject jsonObj = new JSONObject(str);
                JSONArray jsonArry1 = jsonObj.getJSONArray("items");

                int[][] ints1 = new int[jsonArry1.length()][3];

                for(int i = 0; i<jsonArry1.length(); i++){
                    JSONArray jsa1 = jsonArry1.getJSONArray(i);

                    for(int j = 0; j<jsa1.length();j++){
                        ints1[i][j] = jsa1.getInt(j);
                    }

                }

I tested and it works!

Comments

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.