package com.sbm.wll.controller;

import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import com.sbm.wll.dao.UserDao;
import com.sbm.wll.entity.User;
/**
 * 常用JSON数据处理demo
 * @author server
 *
 */
@Controller
public class Json {
	@Autowired
	private UserDao userServiceImpl;

	/**
	 * json格式字符串转json
	 * 
	 * @return
	 */
	@RequestMapping(value = "/json1")
	@ResponseBody
	public String getJson1() {
		String str = "{\"a\":\"b\", \"c\":\"d\"}";
		JSONObject obj = new JSONObject(str);//org.json
		System.out.println("json字符串转json:" + obj);
		return str;
	}

	/**
	 * List<User>转JSON Springboot自动转化 返回值类型为List 
	 * 注释掉的为使用net.sf.json工具做的 ,是正确的
	 * @return
	 */
	@RequestMapping(value = "/json2")
	@ResponseBody
	public List<User> getJson2() {
		List<User> users = null;
		users = userServiceImpl.findAll();
		// net.sf.json.JSONArray array = new
		// net.sf.json.JSONArray().fromObject(users);
		// System.out.println("-----使用.net.sf.json:"+array);
		return users;
	}

	/**
	 * List<User>转JSON 返回值类型为String
	 * 
	 * @return
	 */
	@RequestMapping(value = "/json3")
	@ResponseBody
	public String getJson3() {
		List<User> users = null;
		users = userServiceImpl.findAll();
		JSONArray array = new JSONArray(); // org.json包
		for (int i = 0; i < users.size(); i++) {
			User u = users.get(i);
			JSONObject object = new JSONObject(u);
			// System.out.println("object:" + object);
			array.put(object);
		}
		System.out.println("org.json.Array:to json:" + array);
		return array.toString();
	}

	/**
	 * Map<String,String>转JSON 返回值类型为String 使用.net.sf.json
	 * 
	 * @return
	 */
	@RequestMapping(value = "/json4")
	@ResponseBody
	public String getJson4() {
		Map<String, String> map = new HashMap<>();
		map.put("1", "c100");
		map.put("2", "c233");
		map.put("4", "c556");
		net.sf.json.JSONArray array = new net.sf.json.JSONArray().fromObject(map);
		System.out.println("-----使用.net.sf.json:" + array);
		return array.toString();
	}

	/**
	 * 解析json字符串 json中 包含json数组
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/jx1")
	@ResponseBody
	public String jxJSON1() {
		String jsonString = "{'id':'1','corpcode':'10147','msg':{'time':'16-35','pp':'ddd'}}";
		JSONObject obj = new JSONObject(jsonString); //org.json
		System.out.println("corpcode:" + obj.getString("corpcode"));
		JSONObject msg = new JSONObject(obj.get("msg").toString());// msg不是String,要么get,要么getJSONObj
		System.out.println("msg:" + msg); // 获取msg 里面的json内容
		String time = msg.getString("time");
		System.out.println("msg---time:" + time);
		System.out.println("全部json:" + obj);
		return obj.toString();
	}

	/**
	 * 把JSON数组里面的对象,解析到实体中
	 * 
	 * @return
	 */
	@RequestMapping("/jx2")
	@ResponseBody
	public String jsJSON2() {
		String jString = "[{'id':'111','name':'小明'},{'id':'22','name':'小花'},{'id':'402','name':'小红'}]";
		com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray().parseArray(jString);
		List<User> users = new ArrayList<>();
		for (int i = 0; i < array.size(); i++) {
			User user = new User();
			com.alibaba.fastjson.JSONObject object = array.getJSONObject(i);
			user.setId(Integer.valueOf(object.getString("id")));
			user.setName(object.getString("name"));
			System.out.println("U:" + user.toString());
			users.add(user);
		}
		for (User u : users) {
			System.out.println("----" + u.toString());
		}
		return "";
	}

}
最近下载更多
lironggang  LV38 2023年3月14日
szy0077  LV4 2022年12月8日
17558420274  LV16 2021年8月14日
wyx065747  LV67 2021年7月9日
lipanknight  LV4 2021年3月30日
13043860zj  LV16 2021年1月29日
lovejing  LV7 2020年7月24日
zaizai21312  LV10 2020年6月12日
13072368302  LV8 2020年5月11日
flyingli  LV8 2020年5月6日
最近浏览更多
3263394665  LV9 3月15日
719818732  LV3 3月11日
z96141176 1月17日
暂无贡献等级
微笑刺客  LV15 2023年12月8日
漫步的海星  LV4 2023年9月21日
lironggang  LV38 2023年3月14日
38735466  LV11 2023年1月8日
szy0077  LV4 2022年12月8日
张扬扬109  LV8 2022年11月20日
1529860026  LV24 2022年10月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友