1

I am using org.json.JSONObject package to create a JSONArray by filling up values from db, I can get the values from db and put them a key - pair in json, but at the end of the program: key and values are not appended whereas only the last row of array is converted into json object. I want them to be appended in the final json. Any help will be appreciated.

  try {
    MobileTestClass_Methods.InitializeConfiguration();
    String sqlQuery = "SELECT id, NAME FROM campaign LIMIT 4; ";
    Connection con = MobileTestClass_Methods.CreateSQLConnection();

    String [][] arr = MobileTestClass_Methods
        .ExecuteMySQLQueryReturnsArrayWithColumnName(con, sqlQuery);
    JSONObject json = new JSONObject();
    int totalrow =arr.length;
    int totalcolumn =arr[0].length;

    System.out.println("row: "+totalrow + " col: "+totalcolumn);

    for(int i=1; i<totalrow; i++) {
        for(int j=0; j<totalcolumn; j++) {
            String key = arr[0][j];
            String value = arr[i][j];
            json.put(key, value);
            System.out.println("Key: "+key + "  value: "+value);
        }
    }

    System.out.println("Json: "+json);

    JSONArray array = new JSONArray();
    array.put(json);

    JSONObject main = new JSONObject();
    main.put("result", array);

    System.out.println("Final Json Array: "+main);
  } catch(Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
  }

Output:

  row: 5 col: 2
  Key: id  value: 25256
  Key: NAME  value: megha_video
  Key: id  value: 32168
  Key: NAME  value: Mukesh_13Aug_vpaid
  Key: id  value: 25258
  Key: NAME  value: vast
  Key: id  value: 32167
  Key: NAME  value: SDK-rtb-hudson-130815
  Json: {"id":"32167","NAME":"SDK-rtb-hudson-130815"}
  Final Json Array: {"result":[{"id":"32167","NAME":"SDK-rtb-hudson-130815"}]}

2 Answers 2

3

The result of your query returns always the keys id and NAME, so you're overwriting the values continuously.

If you want to have multiple objects with id and NAME properties you should use a JSONArray and create nested JSONObjects inside:

// ... previous code

String [][] arr = MobileTestClass_Methods.ExecuteMySQLQueryReturnsArrayWithColumnName(con, sqlQuery);
JSONArray json = new JSONArray ();
int totalrow =arr.length;
int totalcolumn =arr[0].length;

System.out.println("row: "+totalrow + " col: "+totalcolumn);

for(int i=1; i<totalrow; i++)
{
    JSONObject row = new JSONObject();
    for(int j=0; j<totalcolumn; j++)
    {
        String key = arr[0][j];
        String value = arr[i][j];
        row.put(key, value);
        System.out.println("Key: "+key + "  value: "+value);
    }
    json.put(row);
}

// ... rest of the code ...
Sign up to request clarification or add additional context in comments.

1 Comment

Well I got idea how to fix that, thanks for that. json.put(row) needs to be changed as json object can't contains another json object so I created a json array to have row json object.
1

Well, I needed to create a json array before starting the loop and then store the json object in the first loop and finally add this json object to json array.

String [][] arr = MobileTestClass_Methods.ExecuteMySQLQueryReturnsArrayWithColumnName(con, sqlQuery);
            JSONObject json = new JSONObject();
            int totalrow =arr.length;
            int totalcolumn =arr[0].length;

            System.out.println("row: "+totalrow + " col: "+totalcolumn);

            JSONArray array = new JSONArray();

            for(int i=1; i<totalrow; i++)
            {
                JSONObject row = new JSONObject();

                for(int j=0; j<totalcolumn; j++)
                {
                    String key = arr[0][j];
                    String value = arr[i][j];
                    row.put(key, value);
                    System.out.println("Key: "+key + "  value: "+value);
                }
                array.put(row);
            }

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.