js 中读取JSON的方法
java
js读取JSON的方法我接触到的有两种:
方法一:函数构造定义法返回
var strJSON = "{name:'json name'}";//获得的JSON
var obj = new Function("return" + strJSON)();//转换后的JSON对象
alert(obj.name);//json namejson
方法二:js中著名的eval函数
var strJSON = "{name:'json name'}";//获得的JSON
var obj = eval( "(" + strJSON + ")" );//转换后的JSON对象
alert(obj.name);//json nameapp
第二种方法须要注意的是,对象表达式{'name':'json name'}必须用"( )"扩住,不然
var strJSON = "{name:'json name'}";
var obj = eval(strJSON);
alert(obj.constructor);//String 构造函数
alert(obj.name);//undefine
必须把对象表达式扩起来eval执行才能生成一个匿名对象!函数
jackson json和JAVA对象之间的转换spa
package com.javamail.test.json; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import com.javamail.test.json.vo.UserVO; public class JsonTest { public static List<UserVO> createUser(){ List<UserVO> list = new ArrayList<UserVO>(); UserVO u = new UserVO(1,"11",1); UserVO u1 = new UserVO(2,"22",1); UserVO u2 = new UserVO(3,"11",1); UserVO u3 = new UserVO(4,"11",1); UserVO u4 = new UserVO(5,"11",1); UserVO u5 = new UserVO(6,"11",1); UserVO u6 = new UserVO(7,"11",1); UserVO u7 = new UserVO(8,"11",1); UserVO u8 = new UserVO(9,"11",1); UserVO u9 = new UserVO(10,"11",1); UserVO u10 = new UserVO(11,"11",1); list.add(u); list.add(u2); list.add(u3); list.add(u4); list.add(u5); list.add(u6); list.add(u7); list.add(u8); list.add(u9); list.add(u10); return list; } public static void main(String[] args) { //把list转成json List<UserVO> list = createUser(); ObjectMapper mapper = new ObjectMapper(); Writer strWriter = new StringWriter(); try { mapper.writeValue(strWriter, list); } catch (JsonGenerationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //把json串转成对应的对象 String json2 = "[{\"age\":1,\"name\":\"11\",\"id\":1},{\"age\":1,\"name\":\"11\",\"id\":3},{\"age\":1,\"name\":\"11\",\"id\":4}]"; System.out.println(json2); try { List<UserVO> uList= mapper.readValue(json2, new TypeReference<List<UserVO>>() {}); for (UserVO userVO:uList) { System.out.print(userVO.getId()+" "); } } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //json转换成单个的JAVA对象 String json3 = "{\"age\":1,\"name\":\"11\",\"id\":1}"; UserVO userVo; try { userVo = mapper.readValue(json3,UserVO.class); System.out.println(userVo.getAge()); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }