简易商品页——JSON

12 JSON

12.1 json是什么

JSON(JavaScript Object Notation) javascript对象表示方法

json只是对象的一种表示方式。

但是现在只要是进行数据传递的地方就用JSON。

12.2 json格式

{ key:value , key:value }     这就是一个json对象。

[ { key:value , key:value } , { key:value , key:value } ]    这就是一个json数组

JSON中国 | JSON中文网

简易商品页——JSON_第1张图片

简易商品页——JSON_第2张图片

简易商品页——JSON_第3张图片

 

 

12.3 表示对象的方式

学员类:

public class Stu{

    private int sid;

    private String sname;

}

java表示对象:

Stu s1 = new Stu();

s1.setSid(1);

s1.setSname(“zhangsan”);

xml文件:

   

         zhangsan

json表示:

{“sid”:1,”sname”:”zhangsan”}

12.4 在js中使用json

<script type="text/javascript">

var stuObj = {"sid":1,"sname":"peterSUN"};

alert(stuObj.sid);

stuObj.sname = "孙旭";

alert(stuObj.sname);

var stuArr = [{"sid":1,"sname":"peterSUN"},{"sid":2,"sname":"孙旭"}];

alert(stuArr.length);

alert(stuArr[0].sid);

script>

12.5 在java中使用Json

实现将java对象转换成json对象。

实现将java集合转换成json数组。

12.5.1 使用第三方组件JSON-LIB

简易商品页——JSON_第4张图片

12.5.2 将java对象转换成json对象

     public static void main(String[] args) {

          List goodsList = new ArrayList();

          Types type = new Types(1, "酒水");

          Goods goods1 = new Goods(1, "伊利", 1000, 100, null, type, null);

          Goods goods2 = new Goods(2, "蒙牛", 2000, 200, null, type, null);

          goodsList.add(goods1);

          goodsList.add(goods2);

         

          //Java对象转换成json对象

          JSONObject jsonObject = JSONObject.fromObject(goods1);

          System.out.println(jsonObject.toString());

         

          //Java集合转换成json数组

          JSONArray jsonArray = JSONArray.fromObject(goodsList);

          System.out.println(jsonArray.toString());

     }

12.6 访问Servlet获取json数据

@WebServlet("/jsonsearch.do")

public class GoodsJsonSearchController extends HttpServlet {

     protected void doGet(HttpServletRequest request, HttpServletResponse response)

               throws ServletException, IOException {

          List goodsList = new ArrayList();

          Types type = new Types(1, "酒水");

          Goods goods1 = new Goods(1, "伊利", 1000, 100, null, type, null);

          Goods goods2 = new Goods(2, "蒙牛", 2000, 200, null, type, null);

          goodsList.add(goods1);

          goodsList.add(goods2);

          response.setCharacterEncoding("utf-8");

          PrintWriter pw = response.getWriter();

          pw.write(JSONArray.fromObject(goodsList).toString());

     }

     protected void doPost(HttpServletRequest request, HttpServletResponse response)

               throws ServletException, IOException {

          request.setCharacterEncoding("utf-8");

          doGet(request, response);

     }

}

<script type="text/javascript">

$(function(){

     $("#searchBtn").click(function(){

          $.ajax({

               type:"post",

               url:"${pageContext.request.contextPath }/jsonsearch.do",

               dataType: "json",

               success:function(msg){

                    for(var i =0;i

                         alert(msg[i].goodsId);

                         alert(msg[i].goodsName);

                         alert(msg[i].goodsType.typeName);

                    }

               }

          });

     });

})

script>

你可能感兴趣的:(json,java,ajax,javascript)