JSON锛?JavaScript Object Notation
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON锛?JavaScript Object Notation相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/googl' title='googl'>googl
vax 缂栧啓 string todo java瀵硅薄 璇█ 杈冨樊 backJSON鏄疛avaScript Object Notation 鐨勭缉鍐欙紝鏄疛S鎻愪緵鐨勪竴绉嶆暟鎹氦鎹㈡牸寮忋€?/span>
1) JSON瀵硅薄鏈川涓婂氨鏄竴涓狫S瀵硅薄锛屼絾鏄繖涓璞℃瘮杈冪壒娈婏紝瀹冨彲浠?strong>鐩存帴杞崲涓哄瓧绗︿覆
2) 渚嬶紝鏈夊涓嬩竴涓狫SON瀵硅薄锛?/span>
銆€銆€鈶?nbsp;{“name”:”sunwukong” , ”age”:18 , ”address”:”beijing” }
銆€銆€鈶?nbsp;杩欎釜瀵硅薄涓湁涓変釜灞炴€ame銆乤ge鍜宎ddress
銆€銆€鈶?nbsp;濡傛灉灏嗚瀵硅薄浣跨敤鍗曞紩鍙峰紩璧蜂簡锛岄偅涔堜粬灏卞彉鎴愪簡涓€涓瓧绗︿覆
銆€銆€鈶?nbsp;‘{“name”:”sunwukong” , ”age”:18 , ”address”:”beijing” }’
銆€銆€鈶?nbsp;鍙樻垚瀛楃涓插悗鏈変竴涓ソ澶勶紝灏辨槸鍙互鍦ㄤ笉鍚岃瑷€涔嬮棿浼犻€掋€?/span>
銆€銆€鈶?span style="color: #0000ff;"> 姣斿锛屽皢JSON浣滀负涓€涓瓧绗︿覆鍙戦€佺粰Servlet锛屽湪Java涓氨鍙互鎶奐SON瀛楃涓茶浆鎹负涓€涓狫ava瀵硅薄
JSON閫氳繃6绉嶆暟鎹被鍨嬫潵琛ㄧず
1) 瀛楃涓?br />? 渚嬪瓙锛?rdquo;瀛楃涓?rdquo;
? 娉ㄦ剰锛氫笉鑳戒娇鐢ㄥ崟寮曞彿
2) 鏁板瓧锛?br />? 渚嬪瓙锛?23.4
3) 甯冨皵鍊硷細
? 渚嬪瓙锛歵rue銆乫alse
4) null鍊?
? 渚嬪瓙锛歯ull
5) 瀵硅薄
? 渚嬪瓙锛歿“name”:”sunwukong”, ”age”:18}
6) 鏁扮粍
? 渚嬪瓙锛歔1,”str”,true]
鍦↗S涓搷浣淛SON
1) 鍒涘缓JSON瀵硅薄
? var json = {“name1”:”value1”,”name2”:”value2” , “name3”:[1,”str”,true]};
? var json = [{“name1”:”value1”},{“name2”:”value2”}];
2) JSON瀵硅薄杞崲涓篔SON瀛楃涓?/span>
? JSON.stringify(JSON瀵硅薄)
3) JSON瀛楃涓茶浆鎹负JSON瀵硅薄
? JSON.parse(JSON瀛楃涓?
//js涓搷浣渏son //1. 缂栧啓涓€涓猨son瀛楃涓? //var jsonStr = 鈥榹"lastName":"Tom","email":"tom@atguigu.com","age":22}鈥? // json瀛楃涓茶浆鎴恓son瀵硅薄 //var jsonObj = JSON.parse(jsonStr); //alert(jsonObj.lastName + "," + jsonObj.email + " , " + jsonObj.age); //json瀵硅薄杞崲鎴恓son瀛楃涓? //var str = JSON.stringify(jsonObj); //alert(str);
鍦↗ava涓搷浣淛SON
1) 鍦↗ava涓彲浠ヤ粠鏂囦欢涓鍙朖SON瀛楃涓诧紝涔熷彲浠ユ槸瀹㈡埛绔彂閫佺殑JSON瀛楃涓诧紝鎵€浠ョ涓€涓棶棰橈紝鎴戜滑鍏堟潵鐪嬪浣曞皢涓€涓狫SON瀛楃涓茶浆鎹㈡垚涓€涓狫ava瀵硅薄銆?/span>
2) 棣栧厛瑙f瀽JSON瀛楃涓叉垜浠渶瑕佸鍏ョ涓夋柟鐨勫伐鍏凤紝鐩墠涓绘祦鐨勮В鏋怞SON鐨勫伐鍏峰ぇ姒傛湁涓夌json-lib銆乯ackson銆乬son銆備笁绉嶈В鏋愬伐鍏风浉姣旇緝json-lib鐨勪娇鐢ㄥ鏉傦紝涓旀晥鐜囪緝宸€傝€孞ackson鍜実son瑙f瀽鏁堢巼杈冮珮銆備娇鐢ㄧ畝鍗曪紝杩欓噷鎴戜滑浠son涓轰緥璁茶В銆?/span>
3) Gson鏄疓oogle鍏徃鍑哄搧鐨勮В鏋怞SON宸ュ叿锛屼娇鐢ㄧ畝鍗曪紝瑙f瀽鎬ц兘濂姐€?/span>
4) Gson涓В鏋怞SON鐨勬牳蹇冩槸Gson鐨勭被锛岃В鏋愭搷浣滈兘鏄€氳繃璇ョ被瀹炰緥杩涜銆?/span>
5) JSON瀛楃涓茶浆鎹负瀵硅薄
String json = "{"name":"寮犱笁","age":18}"; Gson gson = new Gson(); //杞崲涓洪泦鍚?/span> Map<String,Object> stuMap = gson.fromJson(json, Map.class); //濡傛灉缂栧啓浜嗙浉搴旂殑绫讳篃鍙互杞崲涓烘寚瀹氬璞?/span> Student fromJson = gson.fromJson(json, Student.class);
6) 瀵硅薄杞崲涓篔SON瀛楃涓?/span>
Student stu = new Student("鏉庡洓", 23); Gson gson = new Gson(); //{"name":"鏉庡洓","age":23} String json = gson.toJson(stu); Map<String , Object> map = new HashMap<String, Object>(); map.put("name", "瀛欐偀绌?); map.put("age", 30); //{"age":30,"name":"瀛欐偀绌?} String json2 = gson.toJson(map); List<Student> list = new ArrayList<Student>(); list.add(new Student("鍏垝", 18)); list.add(new Student("娌欏儳", 28)); list.add(new Student("鍞愬儳", 38)); //[{"name":"鍏垝","age":18},{"name":"娌欏儳","age":28}, {"name":"鍞愬儳","age":38}] String json3 = gson.toJson(list); // 濡傛灉灏嗕竴涓暟缁勬牸寮忕殑json瀛楃涓茶浆鎹㈡垚java瀵硅薄闇€瑕佺敤鍒? //Gson鎻愪緵鐨勪竴涓尶鍚嶅唴閮ㄧ被锛?TypeToken TypeToken tk= new TypeToken<List<User>>(){}; List<User> list2 = gson.fromJson(json,tk.getType()); System.out.println(list2.get(0));
JQuery 寮傛璇锋眰杩斿洖JSON鏁版嵁
1) Servlet 杩斿洖json鏁版嵁
package com.atguigu.login.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.atguigu.login.beans.Employee; import com.atguigu.login.dao.EmployeeDao; import com.atguigu.login.dao.EmployeeDaoImpl; import com.google.gson.Gson; /** * Servlet implementation class GetEmpsJsonStrServlet */ public class GetEmpsJsonStrServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //鏌ヨ鎵€鏈夌殑鍛樺伐淇℃伅 EmployeeDao employeeDao = new EmployeeDaoImpl(); List<Employee> emps = employeeDao.selectAllEmps(); //杞崲鎴恓son瀛楃涓?/span> Gson gson = new Gson(); String jsonStr = gson.toJson(emps); response.setContentType("text/html;charset=utf-8"); response.getWriter().println(jsonStr); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
2) 椤甸潰涓鐞?json鏁版嵁
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script> <script type="text/javascript"> //js涓搷浣渏son //1. 缂栧啓涓€涓猨son瀛楃涓?/span> //var jsonStr = 鈥榹"lastName":"Tom","email":"tom@atguigu.com","age":22}鈥? // json瀛楃涓茶浆鎴恓son瀵硅薄 //var jsonObj = JSON.parse(jsonStr); //alert(jsonObj.lastName + "," + jsonObj.email + " , " + jsonObj.age); //json瀵硅薄杞崲鎴恓son瀛楃涓?/span> //var str = JSON.stringify(jsonObj); //alert(str); $(function(){ $("#showTable").click(function(){ //鍙戦€佸紓姝ヨ姹傦紝鑾峰彇鍛樺伐鏁版嵁(json鏍煎紡) $.ajax({ url:"GetEmpsJsonStrServlet", type:"post", dataType:"json", success:function(data){ // 鐩存帴灏嗘湇鍔$杩斿洖鐨刯son瀛楃涓茬洿鎺ヨ浆鎹㈡垚json瀵硅薄. var str = "<tr><th>ID</th><th>LastName</th><th>Email</th><th>Gender</th><th>DeptName</th></tr>"; for(var i = 0 ;i <data.length;i++){ var emp = data[i]; str+="<tr align=鈥榗enter鈥?gt;<td>"+emp.id+"</td><td>"+emp.lastName+"</td><td>"+emp.email+"</td><td>"+emp.gender+"</td><td>"+emp.dept.deptName+"</td></tr>"; } $("#empTable").html(str); } }); }); }); </script> </head> <body> <input id="showTable" type="button" value="鏄剧ず鍛樺伐淇℃伅鍒楄〃" /> <h1 align="center"> 鍛樺伐淇℃伅鍒楄〃</h1> <table id="empTable" align="center" border="1px" width="70%" cellspacing="0px"> </table> </body> </html>
以上是关于JSON锛?JavaScript Object Notation的主要内容,如果未能解决你的问题,请参考以下文章
错误水壶 - Json 输入和 Javascript 解析返回 [object Object]
JSON: JavaScript Object Notation
JavaScript 从JSON / Object / Array到STRING
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation, JS 对象标记)
(The application/json Media Type for JavaScript Object Notation (JSON))RFC4627-JSON格式定义