将每条记录存储到json数组java

时间:2017-11-11 06:09:16

标签: java arrays json

我无法将每个结果集记录添加到json数组中。 我有从三个表中获取的数据,我强大了jsonarray1中table1 record1的结果和jsonarray2中table2 record1的结果,依此类推。 最后将组合记录添加到JSON对象,这是一个完整的记录。所以问题是当我打印一个JSON对象时,每个记录提供准确的数据。现在我正在向JSON数组添加一条记录。但是当我打印json数组时,它首先显示准确记录,但是当进入下一步时,它将第三次打印第二次记录三次。但我想要第一,第二,第三等等< / p>

String queryforCustomers="SELECT * FROM customers";
        String queryforCustomer="SELECT * FROM customers where id=?";
        String queryfortax_info="SELECT * FROM `customer_tax_info` where customer_id=?";
        String queryforcustomer_address="SELECT * from customer_address where customer_id=?";
        try
            {
             //for customer
                statement=connection.prepareStatement(queryforCustomers);
                rs1=statement.executeQuery();

                JSONObject customerobj=null;//result of customer information
                JSONObject finalobj1=new JSONObject();//result of every customer records
                JSONArray customersAll=new JSONArray(); //final result(all data)

                while(rs1.next())
                {

                    int id=rs1.getInt("id");
                    System.out.println("Id "+id );
                 // for adding kth element to araay
                                statement=connection.prepareStatement(queryforCustomer);
                                statement.setInt(1, id);
                                rs2=statement.executeQuery();
                                JSONArray arraycustomer=null;
                                arraycustomer=Convertor.convertResultSetIntoJSON(rs2);
                                //System.out.println("Customer Array: "+arraycustomer);
                                rs2.close();
                                statement.close();

                                        statement=connection.prepareStatement(queryfortax_info);
                                        statement.setInt(1, id);
                                        rs2=statement.executeQuery();
                                        JSONArray arrayCustomer_tax_info=null;
                                        arrayCustomer_tax_info=Convertor.convertResultSetIntoJSON(rs2);
                                        //System.out.println("Customer TAx: "+arrayCustomer_tax_info);
                                        rs2.close();
                                        statement.close();


                                statement=connection.prepareStatement(queryforcustomer_address);
                                statement.setInt(1, id);
                                rs3=statement.executeQuery();
                                JSONArray arrayCustomer_address=null;
                                arrayCustomer_address=Convertor.convertResultSetIntoJSON(rs3);
                               // System.out.println("Customer Address: "+arrayCustomer_address);
                                rs3.close();
                                statement.close();
                                finalobj1.put("customer_address",arrayCustomer_address);
                                finalobj1.put("customer_tax_info",arrayCustomer_tax_info);
                                finalobj1.put("customers", arraycustomer);
                                customersAll.put(finalobj1);
                                System.out.println("Final 1 "+finalobj1); 
                                System.out.println("Final "+customersAll.toString());              
                }
                System.out.println("Final "+customersAll.toString()); 
            }
        catch(Exception e)
            {
                e.printStackTrace();

            }




Output {
"customers": [
    {
        "customer_address": [
            {
                "zip": "171004",
                "country": "India",
                "address": "V.PO Chadwal Distt Kathua, Teh Hiranagar  Jammu, Jammu and Kashmir in",
                "as_ship": 1,
                "city": "Shimla",
                "created": "2017-10-10 12:59:45.0"

            }
        ],
        "customer_tax_info": [
            {
                "payment_term": "123",
                "created": "2017-10-10 12:59:45.0",
                "tax_reg_no": "1235",
                "id": 1,
                "customer_id": 1,
                "pan": ""
            }
        ],
        "customers": [
            {
                "website": "",
                "user_id": 1,
                "created": "2017-10-10 12:59:45.0",
                "company_name": "Trinity",
                "mobile": "8872406723",
                "last_name": "Thakur",
                "id": 1,
                "first_name": "Aneh",
                "status": 0
            }
        ]
    },
        {
        "customer_address": [
            {
                "zip": "171004",
                "country": "India",
                "address": "V.PO Chadwal Distt Kathua, Teh Hiranagar  Jammu, Jammu and Kashmir in",
                "as_ship": 1,
                "city": "Shimla",
                "created": "2017-10-10 12:59:45.0"

            }
        ],
        "customer_tax_info": [
            {
                "payment_term": "123",
                "created": "2017-10-10 12:59:45.0",
                "tax_reg_no": "1235",
                "id": 1,
                "customer_id": 1,
                "pan": ""
            }
        ],
        "customers": [
            {
                "website": "",
                "user_id": 1,
                "created": "2017-10-10 12:59:45.0",
                "company_name": "Trinity",
                "mobile": "8872406723",
                "last_name": "Thakur",
                "id": 1,
                "first_name": "Aneh",
                "status": 0
            }
        ]
    },
        {
        "customer_address": [
            {
                "zip": "171004",
                "country": "India",
                "address": "V.PO Chadwal Distt Kathua, Teh Hiranagar  Jammu, Jammu and Kashmir in",
                "as_ship": 1,
                "city": "Shimla",
                "created": "2017-10-10 12:59:45.0"

            }
        ],
        "customer_tax_info": [
            {
                "payment_term": "123",
                "created": "2017-10-10 12:59:45.0",
                "tax_reg_no": "1235",
                "id": 1,
                "customer_id": 1,
                "pan": ""
            }
        ],
        "customers": [
            {
                "website": "",
                "user_id": 1,
                "created": "2017-10-10 12:59:45.0",
                "company_name": "Trinity",
                "mobile": "8872406723",
                "last_name": "Thakur",
                "id": 1,
                "first_name": "Aneh",
                "status": 0
            }
        ]
    }

]

}

1 个答案:

答案 0 :(得分:1)

正如我在您的代码中所看到的那样,您已宣布&#34; customersAll&#34;和&#34; finalobj1&#34;在while循环中,迭代结果集&#34; rs1&#34;即查询&#34; queryforCustomers&#34;

您的代码有什么问题: 你已宣布&#34; customersAll&#34;在while循环之外,这是正确的,但你不应该声明&#34; finalobj1&#34;循环外,因为你正在迭代结果集&#34; rs1&#34;你每次都在&#34; customersAll&#34;中添加相同的对象。对象

您能否修改下面的代码并检查

 JSONObject customerobj=null;//result of customer information
 JSONArray customersAll=new JSONArray(); //final result(all data)
 while(rs1.next())
     {
         JSONObject finalobj1=new JSONObject();
         //"finalobj1" moved to inside of while loop

          . //rest of your code
          .
          .
     }