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瀵硅薄   璇█   杈冨樊   back   

JSON鏄疛avaScript Object Notation 鐨勭缉鍐欙紝鏄疛S鎻愪緵鐨勪竴绉嶆暟鎹氦鎹㈡牸寮忋€?/span>
1) JSON瀵硅薄鏈川涓婂氨鏄竴涓狫S瀵硅薄锛屼絾鏄繖涓璞℃瘮杈冪壒娈婏紝瀹冨彲浠?strong>鐩存帴杞崲涓哄瓧绗︿覆
锛屽湪涓嶅悓璇█涓繘琛屼紶閫掞紝閫氳繃宸ュ叿鍙堝彲浠ヨ浆鎹负鍏朵粬璇█涓殑瀵硅薄銆?/span>
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格式定义