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(); } } }