📜  Boon-从地图

📅  最后修改于: 2020-11-18 09:06:34             🧑  作者: Mango


ObjectMapper类可用于从Map生成json字符串。

以下示例使用ObjectMapper类从Map Object生成JSON字符串。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

public class BoonTester {
   public static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();      
      Map student = new HashMap<>();
      student.put("Name", "Mahesh");
      student.put("RollNo", "21");
      
      Map student1 = new HashMap<>();
      student1.put("Name", "Suresh");
      student1.put("RollNo", "22");
      
      List> studentList = new ArrayList<>();
      studentList.add(student);
      studentList.add(student1);
      
      Map studentMap = new HashMap();
      studentMap.put("students", studentList);
      
      String jsonString = mapper.writeValueAsString(studentMap);
      System.out.println(jsonString);
   }
}

输出

{"students":[{"RollNo":"21","Name":"Mahesh"},{"RollNo":"22","Name":"Suresh"}]}