由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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值