由Map和类生成Json,由Json生成Map

public static void main(String[] args) throws JSONException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
//解析Json
		JsonParse jsonParse=new JsonParse();
		String jsonString=jsonParse.buildJson();
        jsonParse.jsonToMap(jsonString);
        jsonParse.beanToMap(new Employee());
}
package com.parse.sym;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.omg.CORBA.portable.ValueBase;

public class JsonParse {
	

	// Map和类生成Json
	public String buildJson() throws JSONException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("name", "Alexia");
		map1.put("sex", "female");
		map1.put("age", "23");
		Map<String, String> map2 = new HashMap<String, String>();
		map2.put("name", "Edward");
		map2.put("sex", "male");
		map2.put("age", "24");
		List<Map> list = new ArrayList<Map>();
		list.add(map1);
		list.add(map2);
		Employee employee = new Employee();
		employee.setName("wjl");
		employee.setSex("female");
		employee.setAge(24);
		JSONObject jObject=new JSONObject();
		jObject.put("name", "Edward");
		jObject.put("name1", "Edward1");
		System.out.println(jObject.toString());
		/*
		 * 打印结果:{"name":"Edward","name1":"Edward1"}
		 */
		//注意JSONObject和JSONArray装载数据的格式
		JSONArray jmap = new JSONArray();
		jmap.put(map1);
		System.out.println("JSONArray对象数据格式:");
		System.out.println(jmap.toString());
		/*
		 * JSONArray对象数据格式:[{"sex":"female","name":"Alexia","age":"23"}]
		 */
		JSONObject jbean = new JSONObject(beanToMap(employee));
		System.out.println("仅含有Employ对象数据格式:");
		System.out.println(jbean.toString());
		/*
		 * 打印结果:{"sex":"female","name":"wjl","age":"24"}
		 */
		JSONObject jall = new JSONObject();
		jall.put("map", map1);//Map转换成Json
		jall.put("employ",jbean );
		System.out.println("同时含有Employ对象和Map数据格式:");
		System.out.println(jall.toString());
		/*
		 * 打印结果:{"map":{"sex":"female","name":"Alexia","age":"23"},"employ":{"sex":"female","name":"wjl","age":"24"}}
		 */
		return jall.toString();
	}

	// Json生成Map
	public Map<String, Object> jsonToMap(String jsonString) throws JSONException {
		//JSONObject必须以"{"开头
		JSONObject jsonObject = new JSONObject(jsonString);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Iterator<String> iter = jsonObject.keys();
		String key=null;
		Object value=null;
		while (iter.hasNext()) {
			key=iter.next();
			value=jsonObject.get(key);
			resultMap.put(key, value);
		}
		System.out.println(resultMap);
		/*
		 * 打印结果{map={"sex":"female","age":"23","name":"Alexia"}, employ={"sex":"female","age":"24","name":"wjl"}}
		 */
		return resultMap;
	}
	//Bean生成Map
	public Map<String, Object> beanToMap(Object bean) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		Map<String, Object> result=new HashMap<String, Object>();
		Method[] methods = bean.getClass().getDeclaredMethods();
		for (Method method:methods) {
			if (method.getName().startsWith("get")) {
				String field=method.getName();
				field=field.toLowerCase().substring(3);
				Object value=method.invoke(bean, null);
				result.put(field, value==null?"":value.toString());
			}
		}
		return result;
	}
}
package com.parse.sym;

public class Employee {
	private String name;
	private String sex;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

你可能感兴趣的:(json)