[[+content_image]]
N
N
Nikolay Baranenko2017-06-12 14:49:28
Java
Nikolay Baranenko, 2017-06-12 14:49:28

Why are there double [['s when generating json?

Hello.
Probably a dumb question, but I'll ask it anyway.
There is a table

Employee    Input_Weight1 Input_Weight2 Input_Weight3
      сотрудник1 0.2          0.4          0.3
      сотрудник2 0            0            0
      сотрудник3 0            0            0

based on table data
List<String> Employee = new ArrayList<>();
        List<String> Input_Weight1 = new ArrayList<>();
        List<String> Input_Weight2 = new ArrayList<>();
        List<String> Input_Weight3 = new ArrayList<>();

        Employee.add("сотрудник1");
        Employee.add("сотрудник2");
        Employee.add("сотрудник3");
        Input_Weight1.add("0.2");
        Input_Weight1.add("0");
        Input_Weight1.add("0");
        Input_Weight2.add("0.4");
        Input_Weight2.add("0");
        Input_Weight2.add("0");
        Input_Weight3.add("0.3");
        Input_Weight3.add("0");
        Input_Weight3.add("0");

        Employee3 employee3=null;
        JSONArray jsonobj = new JSONArray();
        List<Employee3> map_list=new ArrayList<>();
        for (int i=0; i<Employee.size(); i++){
            employee3=new Employee3();
            employee3.setEmployee(Employee.get(i));
            employee3.setInput_Weight1(Input_Weight1.get(i));
            employee3.setInput_Weight2(Input_Weight2.get(i));
            employee3.setInput_Weight3(Input_Weight3.get(i));
            map_list.add(employee3);
           // System.out.println(Employee.get(i));
        }
        jsonobj.put(map_list);
        Gson gson1 = new Gson();
        System.out.println(gson1.toJson(jsonobj));

class Employee3
public class Employee3 {

private String Employee;
private String Input_Weight1;
private String Input_Weight2;
private String Input_Weight3;

    public String getEmployee() {
        return Employee;
    }

    public void setEmployee(String employee) {
        Employee = employee;
    }

    public String getInput_Weight1() {
        return Input_Weight1;
    }

    public void setInput_Weight1(String input_Weight1) {
        Input_Weight1 = input_Weight1;
    }

    public String getInput_Weight2() {
        return Input_Weight2;
    }

    public void setInput_Weight2(String input_Weight2) {
        Input_Weight2 = input_Weight2;
    }

    public String getInput_Weight3() {
        return Input_Weight3;
    }

    public void setInput_Weight3(String input_Weight3) {
        Input_Weight3 = input_Weight3;
    }
}

resulting in the following result
[[{"Employee":"сотрудник1","Input_Weight1":"0.2","Input_Weight2":"0.4","Input_Weight3":"0.3"},{"Employee":"сотрудник2","Input_Weight1":"0","Input_Weight2":"0","Input_Weight3":"0"},{"Employee":"сотрудник3","Input_Weight1":"0","Input_Weight2":"0","Input_Weight3":"0"}]]

Where is the mistake? how to get rid of double ]] and how to make them single?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Denis Zagaevsky, 2017-06-12
@drno-reg

Because there is no need to interfere with Gson and the standard json engine. You yourself created an array, stuffed a sheet into it (which is also an array in json) and then serialized this byaku using Gson'a.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question