该JSON 是一种轻量级的,基于文本和语言无关的 数据交换格式。JSON可以表示两种结构化类型,如对象 和数组。对象是键/值对的无序 集合,而数组是值的有序序列 。
我们可以使用org.json.simple.JSONValue的t oJSONString()方法(静态)将Map转换为JSON对象。 它有两个重要的静态方法:writeJSONString()方法将对象编码为JSON文本并将其写出;escape()方法用于转义特殊字符和转义引号 \,/,\ r,\ n,\ b,\ f,\ t。
import java.util.*;
import org.json.simple.JSONValue;
public class ConvertMapJSONTest {
   public static void main(String[] args) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("1", "India");
      map.put("2", "Australia");
      map.put("3", "England");
      map.put("4", "South Africa");
      String jsonStr = JSONValue.toJSONString(map); // converts Map to JSON
      System.out.println(jsonStr);
   }
}输出结果
{"1":"India","2":"Australia","3":"England","4":"South Africa"}