public class JsonUtil {
/**
* 从一个JSON 对象字符格式中得到一个java对象 说明:Bean的无参构造函数一定要写, 否则会报:
* net.sf.json.JSONException: java.lang.NoSuchMethodException
*
* @param jsonString
* @param pojoCalss
* @return
*/
public static Object getObjectFromJsonString(String jsonString,
Class pojoCalss) {
Object pojo;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
pojo = JSONObject.toBean(jsonObject, pojoCalss);
return pojo;
}
/**
* 将java对象转换成json字符串
*
* @param javaObj
* @return
*/
public static String getJsonStringFromObject(Object javaObj) {
JSONObject json;
json = JSONObject.fromObject(javaObj);
return json.toString();
}
/**
* 从json HASH表达式中获取一个map
*
* @param jsonString
* @return
*/
@SuppressWarnings("unchecked")
public static Map getMapFromJsonString(String jsonString) {
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Iterator keyIter = jsonObject.keys();
String key;
Object value;
Map valueMap = new HashMap();
while (keyIter.hasNext()) {
key = (String) keyIter.next();
value = jsonObject.get(key);
valueMap.put(key, value);
}
return valueMap;
}
/**
* 从Map对象得到Json字串
*
* @param map
* @return
*/
public static String getJsonStringFromMap(Map map) {
JSONObject json = JSONObject.fromObject(map);
return json.toString();
}
/**
* 从json字串中得到相应java数组
*
* @param jsonString
* like "[\"李斯\",100]"
* @return
*/
public static Object[] getObjectArrayFromJsonString(String jsonString) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
return jsonArray.toArray();
}
/**
* 将list转换成Array
*
* @param list
* @return
*/
public static Object[] getObjectArrayFromList(List list) {
JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toArray();
}
/**
* 用JSONStringer构造一个JsonString
*
* @param m
* @return
*/
public static String buildJsonString(Map m) {
JSONStringer stringer = new JSONStringer();
stringer.object();
for (Object key : m.keySet()) {
stringer.key((String) key)
.value((String)m.get(key));
}
stringer.key("phone");
//begin nesting a array
stringer.array();
stringer.value("13998098000");
stringer.value("8765432");
//nestring object in array
stringer.object();
stringer.key("ppcall");
stringer.value(53881);
stringer.endObject();
stringer.value("13990980980");
//end nesting a array
stringer.endArray();
stringer.endObject();
return stringer.toString();
}
public static void printMap(Map map) {
for (Object key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
public static void main(String[] args) {
Map m = new HashMap() {
{
put("JSon", "HelloWorld");
put("Flex", "Ok");
}
};
System.out.println(buildJsonString(m));
System.out
.println(new JSONStringer().object().key("JSON").value(
"Hello, World!").key("Flex").value("OK").endObject()
.toString());
}
}
文章摘抄自:http://log-cd.iteye.com/blog/469498
以下为自己添加:
JSON jar依赖:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.3</version>
<classifier>jdk15</classifier>
</dependency>
以下由于找不到原文引用,无法提供引用地址
1、转化数组和集合
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
输出:
[true,false,true]
List list = new ArrayList();
list.add(“第一个”);
list.add(“第二个”);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
输出:
[“第一个", "第二个"]
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray3);
输出:
["json", "is", "easy"]
2、 转化对象
2.1转化Map
Map map = new HashMap();
map.put("name","json");
map.put("bool",Boolean.TRUE);
map.put("int",new Integer(1));
map.put("arr",new String[]{"a","b"});
map.put("func","function(i){return this.arr[i];}");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
输出:
["name": "json", "bool":true, "int",1, "arr":[ "a", "b"], "func":function(i){return this.arr[i];}]
2.2转化Bean
从Beans到JSON:
public class MyBean {
private String name = "json";
private int pojoId = 1;
private String func1 = "function(i){return this.options[i]}";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
//以下为get、set方法...
}
MyBean bean = new MyBean();
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
输出:
{"func1":function(i){return this.options[i]},"pojoId":1,"name":"json","func2":function(i){ return this.options[i]; }
从JSON到Beans
String myjson = "{name=\"json\",bool:true,int:1,double:2.2,function:function(a){return a;},array:[1,2]}";
JSONObject json1 = JSONObject.fromString(myjson);
Object bean1 = JSONObject.toBean(json1);
2.3由JSON生成XML
JSONObject json = new JSONObject(true);
XMLSerializer xmlSerializer = new XMLSerializer();
String xml = xmlSerializer.write(json);
System.out.println("xml:" + xml);
输出:
<?xml version="1.0" encoding="UTF-8"?>
<o null="true"/>
JSONObject json2 = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
String xml2 = xmlSerializer.write(json2);
System.out.println("xml2:" + xml2);
输出:
<?xml version="1.0" encoding="UTF-8"?> <o>
<bool type="boolean">true</bool>
<int type="number">1</int>
<name type="string">json</name>
</o>
JSONArray json3 = JSONArray.fromObject("[1,2,3]"); String xml3 = xmlSerializer.write(json3); System.out.println("xml3:" + xml3);
输出:
<?xml version="1.0" encoding="UTF-8"?> <a>
<e type="number">1</e>
<e type="number">2</e>
<e type="number">3</e>
</a>