JSON學習筆記

來源:互聯網
上載者:User

標籤:w3c   page   spring   doc   expires   from   nbsp   UI   定義   

1.匯入所需jar包2.java類中的常用json字串定義及拆分    ====第一種,簡單的JSON數組字串拆分    //定義一個JSON-String字串的數組        String s="[\"1\",\"2\"]";        //把對象轉換為json串        JSONArray ja=JSONArray.fromObject(s);        //列印json的長度        System.out.println(ja.size());            =====第二種,稍微複雜的json對象加數組的拆分            //定義string字串        String objJson="{\"username\":\"ls\",hobby:[\"讀書\",\"寫字\"]}";        //把字串轉換為json對象        JSONObject jar=JSONObject.fromObject(objJson);        //擷取JSONObject對象中愛好為hobby數組,因愛好有多個,所以是數組        JSONArray hobby=jar.getJSONArray("hobby");        //擷取數組中的第一個元素,下標從0開始,所以第一個元素為0        String string = hobby.get(0).toString();        //列印結果        System.out.println(string);            =====第三種,複雜的json數組中包含多個對象,對象中包含多個數組的拆分        //定義複製的JSON-String字串數組,數組記憶體在兩個對象        String objs="[{\"username\":\"ls\",hobby:[\"讀書\",\"寫字\"]},{\"username\":\"zs\",hobby:[\"讀書\",\"玩\"]}]";        //把字串轉換為json數組        JSONArray ob=JSONArray.fromObject(objs);        //擷取json數組中的第二個對象        JSONObject jn1=(JSONObject)ob.get(1);        //擷取第二個json對象中hobby屬性        JSONArray hos=jn1.getJSONArray("hobby");        System.out.println(hos.get(1).toString());        3.jsp網頁中的常用JSON    1.定義在Script標籤中        1.json數組的定義            var arr=["1","2"];            //輸出數組的長度            alert(arr.length);                2.<script>                //定義一個對象,對象中包含數組                var user={                    "username":"zs",                    "password":"123",                    "age":"18",                    "hobby":["讀書","寫字"]                }                //對象的輸出方式,指接對象名.屬性                alert(user.username);                //對象中的屬性數組輸出方式:對象.屬性[第幾個元素]                alert(user.hobby[0]);          </script>                   3.<script>                 //聲明一個數組,數組中包含兩個屬性                var proper=[                    {"userName":"lssssss",                    "paward":"345"},                    {"userName":"lss",                    "paward":"789"}                ]                //輸出數組中的第二個對象的userName屬性                alert(proper[1].userName);           </script>           JSON學習代碼如下:
//測試類別中的JSON
package cn.et.springmvc.lesson05;import net.sf.json.JSON;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class Test { public static void main(String[] args) { //定義一個JSON-String字串的數組 String s="[\"1\",\"2\"]"; //把對象轉換為json串 JSONArray ja=JSONArray.fromObject(s); //列印json的長度 System.out.println(ja.size()); //定義string字串 String objJson="{\"username\":\"ls\",hobby:[\"讀書\",\"寫字\"]}"; //把字串轉換為json對象 JSONObject jar=JSONObject.fromObject(objJson); //擷取JSONObject對象中愛好為hobby數組,因愛好有多個,所以是數組 JSONArray hobby=jar.getJSONArray("hobby"); //擷取數組中的第一個元素,下標從0開始,所以第一個元素為0 String string = hobby.get(0).toString(); //列印結果 System.out.println(string); //定義複製的JSON-String字串數組,數組記憶體在兩個對象 String objs="[{\"username\":\"ls\",hobby:[\"讀書\",\"寫字\"]},{\"username\":\"zs\",hobby:[\"讀書\",\"玩\"]}]"; //把字串轉換為json數組 JSONArray ob=JSONArray.fromObject(objs); //擷取json數組中的第二個對象 JSONObject jn1=(JSONObject)ob.get(1); //擷取第二個json對象中hobby屬性 JSONArray hos=jn1.getJSONArray("hobby"); System.out.println(hos.get(1).toString()); }}

 

 
//jsp網頁中的JSON的常用處理19:46:57
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘json.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> //在script標籤中定義json數組 var arr=["1","2"]; alert(arr.length); //定義一個對象,對象中包含數組 var user={ "username":"zs", "password":"123", "age":"18", "hobby":["讀書","寫字"] } //對象的輸出方式,指接對象名.屬性 alert(user.username); //對象中的屬性數組輸出方式:對象.屬性[第幾個元素] alert(user.hobby[0]); //聲明一個數組,數組中包含兩個屬性 var proper=[ {"userName":"lssssss", "paward":"345"}, {"userName":"lss", "paward":"789"} ] //輸出數組中的第二個對象的userName屬性 alert(proper[1].userName); </script> </head> <body> This is my JSP page. <br> </body></html>

 


 

JSON學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.