JSON学习笔记

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>



原文地址:https://www.cnblogs.com/xushirong/p/6964490.html