zoukankan      html  css  js  c++  java
  • JSON

    JSON

    JavaScript Object Notation,⼀种轻量级数据交互格式,完成 js 与 Java 等后端开发语⾔对象数据之间的转换。

    客户端和服务器之间传递对象数据,需要用JSON格式。

    复制代码
    package com.southwind.entity;
    public class User {
     private Integer id;
     private String name;
     private Double score;
     public Integer getId() {
     return id;
     }
     public void setId(Integer id) {
     this.id = id;
     }
     public String getName() {
     return name;
     }
     public void setName(String name) {
     this.name = name;
     }
     public Double getScore() {
     return score;
     }
     public void setScore(Double score) {
     this.score = score;
     }
     public User(Integer id, String name, Double score) {
     this.id = id;
     this.name = name;
     this.score = score;
     }
    }
    User user = new User(1,"张三",96.5);
    复制代码
    var user = {
     id:1,
     name:"张三",
     score:96.5
    }
    复制代码
    package com.southwind.servlet;
    import com.southwind.entity.User;
    import net.sf.json.JSONObject;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    @WebServlet("/test")
    public class TestServlet extends HttpServlet {
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
     User user = new User(1,"张三",96.5);
     //将 Java 对象转为 JSON 格式
     resp.setCharacterEncoding("UTF-8");
     JSONObject jsonObject = JSONObject.fromObject(user);
     resp.getWriter().write(jsonObject.toString());
     }
    }
    复制代码
    复制代码
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
     <title>Title</title>
     <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
     <script type="text/javascript">
     $(function(){
       var btn = $("#btn");
       btn.click(function(){
       $.ajax({
         url:'/test',
         type:'post',
         dataType:'json',
         success:function(data){
           $("#id").val(data.id);
           $("#name").val(data.name);
           $("#score").val(data.score);
           }
       });
     });
     })
     </script>
    </head>
    <body>
     编号:<input id="id" type="text"/><br/>
     姓名:<input id="name" type="text"/><br/>
     成绩:<input id="score" type="text"/><br/>
     <input id="btn" type="button" value="提交"/>
    </body>
    </html>
    复制代码

    在开发微信小程序的时候,基本上使用的都是JSON语言

    所以看了看JSON的格式以及基本用法

  • 相关阅读:
    PhpStorm Swoole 和 CI 代码自动补全
    python正则表达式匹配多行
    ES6 Template Strings(转)
    IntelliJ隐藏特定后缀文件
    网络游戏术语(转)
    mac查看当前调用tcp的进程并关闭指定进程
    袭击Mercurial SCM(HG)
    T 泛型转换
    UiAutomator源代码分析之UiAutomatorBridge框架
    ASP.NET MVC 入门8、ModelState与数据验证
  • 原文地址:https://www.cnblogs.com/lmygq1728/p/14160923.html
Copyright © 2011-2022 走看看