当前位置: 首页 > 工具软件 > Remark-java > 使用案例 >

Java基础--Java中JSON的处理

冀弘济
2023-12-01

1、引入jar包

commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
ezmorph-1.0.4.jar
json-lib-2.2.3-jdk13.jar


2、JSONTest类

package com.jspwb.framework.test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.jspwb.framework.dbutil.JdbcTemplate;
import com.jspwb.framework.dbutil.inteface.RowMapper;
import com.jspwb.sysmanage.bean.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONTest {

	public static String buildJSON(Object object) {
		//单个User,或者hashmap转JSON
//		JSONObject jsonObject = JSONObject.fromObject(object);
		//List转JSON
		JSONArray arrayJSON = JSONArray.fromObject(object);
		return arrayJSON.toString();
	}
	
	public static void parseJSON(String jsonstr) {
		//JSON字符串,取值
		JSONArray jsonArray = JSONArray.fromObject(jsonstr);
		for (int i = 0; i < jsonArray.size(); i++) {
			
			System.out.println(jsonArray.getJSONObject(i).get("userId"));
			System.out.println(jsonArray.getJSONObject(i).get("userName"));
			
		}
	}
	
	
	public static void main(String[] args) {
		//构建List对象,可以是List<HashMap<String, Object>>   也可以是List<User>
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		String sql = "SELECT * FROM `user` where user_id >= ? and user_id < ?";
		String sql1 = "SELECT `user_id`,`user_name`,`status`,`mobile` FROM `user` where user_id >= ? and user_id < ?";
		Object[] params = {2,6};
		List<HashMap<String, Object>> userHashMapList = jdbcTemplate.queryForList(sql, params);

		
		List<User> userList = jdbcTemplate.queryForList(new RowMapper<User>(
				) {
					@Override
					public User mappingRow(ResultSet rs, int rownum) throws SQLException {
						User user = new User();
						user.setUserId(rs.getInt("user_id"));
						user.setUserName(rs.getString("user_name"));
						user.setStatus(rs.getInt("status"));
						user.setMobile(rs.getString("mobile"));
						return user;
					}
		}, sql1, params);
		
		//List对象转换为JSON字符串
		System.out.println("=====================List<HashMap<String, Object>>解析出的JSON字符串=====================");
		System.out.println(buildJSON(userHashMapList));
		System.out.println("=====================List<User>解析出的JSON字符串=====================");
		System.out.println(buildJSON(userList));
		
		//通过JSON字符串,取得变量值
		System.out.println("=====================根据JSON字符串,取值=====================");
		String jString = buildJSON(userList);
		parseJSON(jString);
	}
}

3、
开始尝试连接MySQL数据库!
连接MySQL数据库成功!
数据库连接已关闭!
开始尝试连接MySQL数据库!
连接MySQL数据库成功!
数据库连接已关闭!
=====================List<HashMap<String, Object>>解析出的JSON字符串=====================
[{"password":"1234","otelephone":"18812341234","user_id":2,"role_id":1,"user_name":"FYLi","mobile":"18611112222","htelephone":"18812341234","remark":"1","tv_update":{"date":28,"day":1,"hours":17,"minutes":6,"month":10,"nanos":0,"seconds":58,"time":1480324018000,"timezoneOffset":-480,"year":116},"dept_id":1,"status":1},{"password":"1234","otelephone":"12","user_id":3,"role_id":2,"user_name":"mm","mobile":"177","htelephone":"11","remark":"哈哈","tv_update":{"date":28,"day":1,"hours":0,"minutes":0,"month":10,"nanos":0,"seconds":0,"time":1480262400000,"timezoneOffset":-480,"year":116},"dept_id":-1,"status":1},{"password":"1234","otelephone":"11","user_id":4,"role_id":2,"user_name":"mengmmmm","mobile":"11","htelephone":"11","remark":"11","tv_update":{"date":29,"day":2,"hours":0,"minutes":0,"month":10,"nanos":0,"seconds":0,"time":1480348800000,"timezoneOffset":-480,"year":116},"dept_id":-1,"status":1}]
=====================List<User>解析出的JSON字符串=====================
[{"deptId":0,"htelephone":"","mobile":"18611112222","otelephone":"","password":"","remark":"","roleId":0,"status":1,"tvUpdate":null,"userId":2,"userName":"FYLi"},{"deptId":0,"htelephone":"","mobile":"177","otelephone":"","password":"","remark":"","roleId":0,"status":1,"tvUpdate":null,"userId":3,"userName":"mm"},{"deptId":0,"htelephone":"","mobile":"11","otelephone":"","password":"","remark":"","roleId":0,"status":1,"tvUpdate":null,"userId":4,"userName":"mengmmmm"}]
=====================根据JSON字符串,取值=====================
2
FYLi
3
mm
4
mengmmmm


 类似资料: