0

its my first time asking here instead of read. I'm developing a JAVA software that copies a database into a JSON file. My problem is that I'm only getting copies the last line that gets to database.

Theres my CODE

public void getData() throws IOException {
    try {

        String query = "select * from Productos";
        rs = st.executeQuery(query);
        System.out.println("Prueba de funcionamiento");
        JSONObject jobj = new JSONObject();
        while(rs.next()) {
            String cdpt= rs.getString("Cod.RefPro");
            String name= rs.getString("Nombre");
            String desc = rs.getString("Descripcion");
            String prec = rs.getString("PrecioNeto");
            String pvp = rs.getString("pvp");
            String cdp = rs.getString("CodRefProv");
            String fam = rs.getString("Familia");
            String exis = rs.getString("Existencias");

            System.out.println(cdpt);


             JSONArray list = new JSONArray();
             JSONObject jobja = new JSONObject();
             JSONObject jobjb = new JSONObject();
             JSONObject jobjc = new JSONObject();
             JSONObject jobjd = new JSONObject();
             JSONObject jobje = new JSONObject();
             JSONObject jobjf = new JSONObject();
             JSONObject jobjg = new JSONObject();
             JSONObject jobjh = new JSONObject();


             jobja.put("crf", cdpt);
             jobjb.put("name", name);
             jobjc.put("desc", desc);
             jobjd.put("pve", prec);
             jobje.put("pvp", pvp);
             jobjf.put("cdp", cdp);
             jobjg.put("familia", fam);
             jobjh.put("existencias", exis);
             list.add(jobja);
             list.add(jobjb);
             list.add(jobjc);
             list.add(jobjd);
             list.add(jobje);
             list.add(jobjf);
             list.add(jobjg);
             list.add(jobjh);
             jobj.put("Productos", list);



        }


        // try-with-resources statement based on post comment below :)
        try (FileWriter file = new FileWriter("N:\\file.json")) {
            file.write(jobj.toJSONString());
            file.flush();
            System.out.println("Successfully Copied JSON Object to File...");
            System.out.println("\nJSON Object: " + jobj);
        }
    }catch(Exception ex) {
        System.out.println("Error: "+ex);
    }
}

}

I'll be so grateful is someone helps me to solve this problem

Gyswus
  • 29
  • 8

3 Answers3

4

JSONObject extends Map.

when you call put, an element is added to the map.

In your case, every element is added using the key "Productos".

Each time you put a new element, it overwrites the previously added element because the key is the same for both.

DwB
  • 37,124
  • 11
  • 56
  • 82
1

You are overwriting the list at Product key value every time loop runs.

So just replace

jobj.put("Productos", list);

With

  jobj.put("Productos"+i++, list);

and define i outside the loop.

after replacing it.Your code goes something like this

public void getData() throws IOException {
        try {

                String query = "select * from Productos";
                rs = st.executeQuery(query);
                System.out.println("Prueba de funcionamiento");
                JSONObject jobj = new JSONObject();
                String cdpt="",name="",desc="",precm="",pvp="",cdp="",fam="",exis="";
                JSONArray list = new JSONArray();
                JSONObject[7] jobj=new JSONObject();
                int i=0;
                while(rs.next()) {
                        cdpt= rs.getString("Cod.RefPro");
                        name= rs.getString("Nombre");
                        desc = rs.getString("Descripcion");
                        prec = rs.getString("PrecioNeto");
                        pvp = rs.getString("pvp");
                        cdp = rs.getString("CodRefProv");
                        fam = rs.getString("Familia");
                        exis = rs.getString("Existencias");

                        // System.out.println(cdpt);

                        for (int i=0; i<=jobj.length; i++)
                                jopv[i]=new JSONObject();

                        jobj[0].put("crf", cdpt);
                        jobj[1].put("name", name);
                        jobj[2].put("desc", desc);
                        jobj[3].put("pve", prec);
                        jobj[4].put("pvp", pvp);
                        jobj[5].put("cdp", cdp);
                        jobj[6].put("familia", fam);
                        jobj[7].put("existencias", exis);

                        for(int i=0; i<=jobj.lenght; j++)
                                list.add(jobj[i]);

                        jobj.put("Productos"+i++, list);
                }


                // try-with-resources statement based on post comment below :)
                try (FileWriter file = new FileWriter("N:\\file.json")) {
                        file.write(jobj.toJSONString());
                        file.flush();
                        System.out.println("Successfully Copied JSON Object to File...");
                        System.out.println("\nJSON Object: " + jobj);
                }
        }catch(Exception ex) {
                System.out.println("Error: "+ex);
        }
}

Hope this one is helpful.

Nitin
  • 1,280
  • 1
  • 13
  • 17
0

jobj is a Map

jobj.put("Productos", list);

With this line you are putting only the last list into jobj with key "Productos".

I think you need a list for jobj

kjp
  • 3,086
  • 22
  • 30