日韩精品欧美激情国产一区_中文无码精品一区二区三区在线_岛国毛片AV在线无码不卡_亞洲歐美日韓精品在線_使劲操好爽好粗视频在线播放_日韩一区欧美二区_八戒八戒网影院在线观看神马_亚洲怡红院在线色网_av无码不卡亚洲电影_国产麻豆媒体MDX

java生成json格式的數(shù)據(jù)

時(shí)間:2020-01-17 15:45:55 類型:JAVA
字號(hào):    

JAVA生成json格式的數(shù)據(jù), 這里使用import org.json.JSONObject;

先加載所需要的jar包

json.zip

response.setContentType("text/html;charset=UTF-8");
	    PrintWriter out = response.getWriter();
		JSONObject jsonObject =  new JSONObject();
		jsonObject.put("names","莊子");
		jsonObject.put("sex", "男");
		String[] hobby = {"游泳","打籃球"};
		jsonObject.put("hobbies", Arrays.asList(hobby)); //添加數(shù)組
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		map.put("eng",100);
		map.put("math",99);
		map.put("chinese",98);
		jsonObject.put("score", map);
		System.out.println(jsonObject);//添加HashMap對(duì)象
		out.println(jsonObject);

顯示結(jié)果如下:

{"score":{"chinese":98,"math":99,"eng":100},"names":"莊子","hobbies":["游泳","打籃球"],"sex":"男"}

解析結(jié)果如下:


1.jpg

AJAX請(qǐng)求結(jié)果如下:

2.jpg

方法二:加載阿里的fastjson2包

fastjson2-2.0.12.graal.zip

HashMap<String, Object> hm = new HashMap<>();
        hm.put("name","小強(qiáng)");
        hm.put("sex","男");
        String[] h = {"牛奶","香蕉"};
        hm.put("h",h);
String json = JSON.toJSONString(hm);
        response.getWriter().println(json);


<