標籤:傳輸資料 hash 為什麼 前台 字元 nic 依賴 json對象 字串
一、為什麼要使用JSONArray和JSONObject
1、後台 --》前台 能夠把java對象和集合轉化成json字串格式,這樣在前台的ajax方法中能夠直接轉化成json對象使用 ,從後台向前台傳值
2、前台 --》使用JSON.stringify(mdses)方法把js對象,數組。轉化成json字串 ,後台使用JSONArray或JSONObject 轉化成 java對象或集合
有助於前端參數傳遞到後端。和後端取值y
3、json如今以成為大多傳輸資料的載體
二、後台解析前台ajax提交資料,使用較少一般都把list 或map轉化為json形式的字串傳值到前台的ajax中:
//依據接收的json字串來解析字串中所包括的資料和資料對象@Testpublic void parsJsonFromFront(){//接收到的jsonString result = "[{\"username\": \"your name\", \"user_json\": {\"username\": \"your name\", \"nickname\": \"your nickname\"}}]"; //依據字串陳//依據字串產生JSON對象//假設是數組的形式 就使用 JSONArray.fromObject(result) 轉化為JSONArray 對象//假設是json對象 就是使用JSONObject.fromObject(result) 轉化為JSONObject 對象JSONArray resultArray = JSONArray.fromObject(result);JSONObject jsonObject = resultArray.getJSONObject(0);JSONObject user_json = jsonObject.getJSONObject("user_json");//依據key值取值String username = user_json.getString("username");String nickname = user_json.getString("nickname");System.out.println("username =" + username + ":" + "nickname = " + nickname);//console : username =your name:nickname = your nickname}
三 、後台轉化list 或map轉化為json形式的字串傳值到前台的ajax中
user bean
public class User {private String name;private Adress adress;private List<String> students;public String getName() {return name;}public void setName(String name) {this.name = name;}public Adress getAdress() {return adress;}public void setAdress(Adress adress) {this.adress = adress;}public List<String> getStudents() {return students;}public void setStudents(List<String> students) {this.students = students;}public User(String name, Adress adress, List<String> students) {super();this.name = name;this.adress = adress;this.students = students;}public User() {super();}}
public class Adress {private String streetName;private String district;public String getStreetName() {return streetName;}public void setStreetName(String streetName) {this.streetName = streetName;}public String getDistrict() {return district;}public void setDistrict(String district) {this.district = district;}public Adress(String streetName, String district) {super();this.streetName = streetName;this.district = district;}public Adress() {super();// TODO Auto-generated constructor stub}}
1)bean to json
@Testpublic void beanToJson(){List list = new ArrayList();list.add( "xiaowang" );list.add( "xiaohua" );Adress adress = new Adress("streetName", "district");JSONObject jsonObject = JSONObject.fromObject(new User("name", adress, list));System.out.println(jsonObject.toString());//console :{"adress":{"district":"district","streetName":"streetName"},"name":"name","students":["xiaowang","xiaohua"]}}
2)
@Testpublic void list2Json(){List list = new ArrayList();list.add( "first" );list.add( "second" );JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());}public Map getMap(){ //聲明一個Hash對象並加入資料 Map params = new HashMap();params.put("username", "username");params.put("user_json", "user");return params;}@Testpublic void map2Json(){Map map = getMap();Map map1 = getMap();List list = new ArrayList();list.add(map);list.add(map1);JSONArray jsonArray = JSONArray.fromObject(list);System.out.println("jsonArray--->" + jsonArray.toString());JSONArray jsonArray1 = JSONArray.fromObject(map);System.out.println("jsonArray1--->" + jsonArray1.toString());}
完整:
public class SimpleExample {@Testpublic void list2Json(){List list = new ArrayList();list.add( "first" );list.add( "second" );JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());}public Map getMap(){ //聲明一個Hash對象並加入資料 Map params = new HashMap();params.put("username", "username");params.put("user_json", "user");return params;}@Testpublic void map2Json(){Map map = getMap();Map map1 = getMap();List list = new ArrayList();list.add(map);list.add(map1);JSONArray jsonArray = JSONArray.fromObject(list);System.out.println("jsonArray--->" + jsonArray.toString());JSONArray jsonArray1 = JSONArray.fromObject(map);System.out.println("jsonArray1--->" + jsonArray1.toString());}//依據接收的json字串來解析字串中所包括的資料和資料對象@Testpublic void parsJsonFromFront(){//接收到的jsonString result = "[{\"username\": \"your name\", \"user_json\": {\"username\": \"your name\", \"nickname\": \"your nickname\"}}]"; //依據字串陳//依據字串產生JSON對象//假設是數組的形式 就使用 JSONArray.fromObject(result) 轉化為JSONArray 對象//假設是json對象 就是使用JSONObject.fromObject(result) 轉化為JSONObject 對象JSONArray resultArray = JSONArray.fromObject(result);JSONObject jsonObject = resultArray.getJSONObject(0);JSONObject user_json = jsonObject.getJSONObject("user_json");//依據key值取值String username = user_json.getString("username");String nickname = user_json.getString("nickname");System.out.println("username =" + username + ":" + "nickname = " + nickname);//console : username =your name:nickname = your nickname}@Testpublic void beanToJson(){List list = new ArrayList();list.add( "xiaowang" );list.add( "xiaohua" );Adress adress = new Adress("streetName", "district");JSONObject jsonObject = JSONObject.fromObject(new User("name", adress, list));System.out.println(jsonObject.toString());//console :{"adress":{"district":"district","streetName":"streetName"},"name":"name","students":["xiaowang","xiaohua"]}}}
相關依賴jar下載:http://download.csdn.net/detail/hao007cn/8995465
JSONArray和JSONObject的簡單使用