将列表转换为JSON

时间:2017-09-14 08:43:03

标签: java json

我从DB收到以下列表,我想将其转换为JSON:

Employee e1=new Employee(101,"Ha","De","Acr");
Employee e2=new Employee(102,"D ","Forouzan","Mc");
Employee e3=new Employee(102,"Op","Ga","Wi");
Employee e4=new Employee(101,"YUI","HI","EX");

我想改变它只是它转换上面收到的列表和重复键(101,102)它使用数组创建一个jsoobject;

ex: 101 : {["Ha","De","Acr"],["YUI","HI","EX"]}

3 个答案:

答案 0 :(得分:2)

我尝试了下面的一个并得到了预期的结果:

public static void main(String[] args) {

    Employee e1 = new Employee(101, "Ha", "De", "Acr");
    Employee e2 = new Employee(102, "D ", "Forouzan", "Mc");
    Employee e3 = new Employee(102, "Op", "Ga", "Wi");
    Employee e4 = new Employee(101, "YUI", "HI", "EX");

    List<Employee> employeeList1 = new ArrayList<>();
    employeeList1.add(e1);      
    employeeList1.add(e4);

    List<Employee> employeeList2 = new ArrayList<>();
    employeeList2.add(e2);
    employeeList2.add(e3);

    Map<Integer, ArrayList<Employee>> map = new HashMap<Integer, ArrayList<Employee>>();
    // As of now , I have populated the map directly, but you can place some logic here for putting the values here dynamically.
    map.put(101, (ArrayList<Employee>) employeeList1);
    map.put(102, (ArrayList<Employee>) employeeList2);

    ObjectMapper mapper = new ObjectMapper();
    try {
        String json = mapper.writeValueAsString(map);
        System.out.println(json);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 1 :(得分:0)

我会试着回答。

List<Employee> list = //Our employees
Map x = new HashMap();
for(int y = 0; y < list; y++){
  x.put(list[y].value0, new Array(list[y].value1, list[y].value2, list[y].value3));
}
JSONObject json = new JSONObject();
json.putAll( x );

这应该做你需要的! 试试吧,让我知道。

答案 2 :(得分:0)

您要问的是有点不清楚,因为您的示例输出无效JSON。我在这里假设您要将员工标识符映射到员工列表,您可以使用Map<Integer, List<Employee>>建模。要从此数据结构生成JSON,您需要一个外部库,例如Jackson JSON库。

假设以下Employee

public class Employee {

    private final int id;
    private final String name1;       
    private final String name2;
    private final String name3;

    public Employee(int id, String name1, String name2, String name3) {
        this.id = id;
        this.name1 = name1;
        this.name2 = name2;
        this.name3 = name3;
    }

    public int getId() {
        return id;
    }
    public String getName1() {
        return name1;
    }
    public String getName2() {
        return name2;
    }
    public String getName3() {
        return name3;
    }        
}

编组代码

Employee e1 = new Employee(101,"Ha","De","Acr");
Employee e2 = new Employee(102,"D ","Forouzan","Mc");
Employee e3 = new Employee(102,"Op","Ga","Wi");
Employee e4 = new Employee(101,"YUI","HI","EX");

Map<Integer, List<Employee>> employees = new HashMap<>();
employees.put(101, Arrays.asList(e1, e4));
employees.put(102, Arrays.asList(e2, e3));

String json = new ObjectMapper().writerWithDefaultPrettyPrinter()
    .writeValueAsString(employees);        
System.out.println(json);

你会得到这个JSON:

{
  "102" : [ {
    "id" : 102,
    "name1" : "D ",
    "name2" : "Forouzan",
    "name3" : "Mc"
  }, {
    "id" : 102,
    "name1" : "Op",
    "name2" : "Ga",
    "name3" : "Wi"
  } ],
  "101" : [ {
    "id" : 101,
    "name1" : "Ha",
    "name2" : "De",
    "name3" : "Acr"
  }, {
    "id" : 101,
    "name1" : "YUI",
    "name2" : "HI",
    "name3" : "EX"
  } ]
}

代码的必需Maven依赖项:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.0</version>
</dependency>
<!-- Jackson databinding; ObjectMapper, JsonNode and related classes are here -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.0</version>
</dependency>