首页 > 代码库 > 解析json
解析json
package com.spring.mvc;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.junit.Test;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @description 功能描述:
* @param 参 数:
* @createdate 建立日期: 2014-9-1上午10:01:51
* @projectname 项目名称: spring_mvc
* @packageclass 包及类名: com.spring.mvc.Json.java
*/
public class Json {
// @Test
public void json1(){
String yey = "[{\"id\":1,\"pId\":-1,\"name\":\"根节点\"},{\"id\":2,\"open\":true,\"pId\":1,\"name\":\"你好\"}]";
JSONArray o = JSONArray.fromObject(yey);
List<Map<String,Object>> mapListJson = (List)o;
for (int i = 0; i < mapListJson.size(); i++) {
Map<String,Object> obj=mapListJson.get(i);
for(Entry<String, Object> entry : obj.entrySet()){
String strkey1 = entry.getKey();
Object strval1 = entry.getValue();
System.out.println("KEY:"+strkey1+" --> Value:"+strval1+"\n");
}
}
}
/**
* @projectname 项目名称: spring_mvc
* @packageclass 包及类名: com.spring.mvc.Json.java
* @description 功能描述: 一个key 对应多个map 转换json 串
* @param 参 数:
* @return 返回类型: {"key":{"2":"小黄","1":"小蓝"}}
* @createdate 建立日期: 2014-9-1上午11:22:05
*/
// @Test
public void json2 (){
Map<Object, Map<String, String>> map = new HashMap<Object, Map<String,String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("1", "小蓝");
map1.put("2", "小黄");
map.put("key", map1);
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
}
/**
* @projectname 项目名称: spring_mvc
* @packageclass 包及类名: com.spring.mvc.Json.java
* @description 功能描述: 解析 {"key":{"2":"小黄","1":"小蓝"}} 格式json
* @author 作 者: 周志伟
* @param 参 数:
* @return 返回类型: void
* @createdate 建立日期: 2014-9-1上午11:22:40
*/
@Test
public void json3(){
JSONObject node = JSONObject.fromObject("{\"key\":{\"2\":\"小黄\",\"1\":\"小蓝\"}}");
List<Object> nodes = new ArrayList<Object>();
JSONArray jsons = JSONArray.fromObject(node.get("key"));
List<Object> treeNodes = new ArrayList<Object>();
for (Object o : jsons) {
JSONObject jsonNode = JSONObject.fromObject(o);
treeNodes.add(jsonNode.getString("2"));
treeNodes.add(jsonNode.getString("1"));
nodes.add(treeNodes);
}
for (Object object : treeNodes) {
System.out.println(object);
}
}
/**
* @projectname 项目名称: spring_mvc
* @packageclass 包及类名: com.spring.mvc.Json.java
* @description 功能描述: 解析 {"key":["红","黄","蓝"]} 格式json
* @author 作 者: 周志伟
* @param 参 数:
* @return 返回类型: void
* @createdate 建立日期: 2014-9-1上午11:22:40
*/
@Test
public void json4(){
Map<Object, List<Object>> map = new HashMap<Object, List<Object>>();
List<Object> list = new ArrayList<Object>();
list.add("红");
list.add("黄");
list.add("蓝");
map.put("key", list);
JSONObject json = JSONObject.fromObject(map);//输出json格式
System.out.println(json);
}
/**
* @projectname 项目名称: spring_mvc
* @packageclass 包及类名: com.spring.mvc.Json.java
* @description 功能描述: 解析 {"key":["红","黄","蓝"]} 格式json
* @author 作 者: 周志伟
* @param 参 数:
* @return 返回类型: void
* @createdate 建立日期: 2014-9-1上午11:22:40
*/
@Test
public void json4Type(){
JSONObject node = JSONObject.fromObject("{\"key\":[\"红\",\"黄\",\"蓝\"]}");
JSONArray jsons = (JSONArray) node.get("key");
for (Object object : jsons) {
System.out.println(object);
}
}
public static void list2JsonArry(List list,String dateFormat,HttpServletResponse response){
try{
DateProcessor processor = new DateProcessor();
processor.setPattern(dateFormat);
JsonConfig config = new JsonConfig();
//??????????????????
config.registerJsonValueProcessor(Date.class, processor);
//??list<Object>??????JSON???
JSONArray jsonArray = JSONArray.fromObject(list,config);
System.out.println(jsonArray);
//?????????????
response.setContentType("text/html;charset=utf-8");
PrintWriter out;
out = response.getWriter();
out.print(jsonArray.toString());
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
解析json