JSON在线解析

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
其二同意无色大润发通过 发布于:2024-10-21 13:12 NBTK SGR0201 发布于:2024-08-21 10:55 广西田东PVM0101 发布于:2024-08-08 17:41 鲁花共享运营报告 发布于:2024-05-11 17:19 **全国地表水水质监测系统 断面点位数据集** 发布于:2024-03-02 18:33 AliRedPacket首页红包 发布于:2023-12-23 15:41 main主程序 发布于:2023-12-23 15:18 扬秋OL(autojs) 发布于:2023-12-23 15:43 json教师各项分数 发布于:2023-12-09 16:39 员工工作量 发布于:2023-11-07 15:17 浪潮POST 发布于:2023-10-30 11:22 报账单信息 发布于:2023-10-20 14:32 我带会员单位岁啊哈幅度缺乏撒网缺乏v 发布于:2022-09-27 08:28 环保token 发布于:2022-05-12 21:12 流水线的保存的json数据,把里面所有的job的几个信息摘出来,取出每个job的jobName、jobType、categoryId、realJob 内容。 发布于:2022-04-22 19:17 爱上CAD 发布于:2022-02-17 23:03 中国省市区 json 发布于:2021-10-15 13:39 供应商带出币种 发布于:2021-07-23 10:19 拉取采购收货 发布于:2021-07-13 16:14 解析IP地址 发布于:2021-04-12 20:45 OA --> CMDB_DCOS的北向数据 发布于:2021-02-02 15:00 测试json 是否正确 发布于:2021-01-13 11:06 python题库 发布于:2021-01-06 10:31 json 解析 发布于:2020-10-18 15:05 电池图所需数据 发布于:2020-10-08 18:32 电池图所需数据 发布于:2020-09-20 13:57 电池图所需数据 发布于:2020-09-20 13:57 JSON在线解析,JSON格式化 发布于:2020-09-16 12:10 电池图所需数据 发布于:2020-09-02 11:35 电池图所需数据 发布于:2020-08-25 17:50 建立一个mock测试 json 数据。 发布于:2020-08-22 10:32 地理缩写对照 发布于:2020-07-08 13:28 v2ray-config 发布于:2021-05-11 13:03 JSON在线解析,JSON格式化 发布于:2020-06-20 15:04 [更多]
显示目录

SP + jQuery使用Ajax方式调用JSON



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

JSP + jQuery使用Ajax方式调用JSON

在这里分享以供参考,具体的实现以及代码如下所示

<script type="text/javascript" src="jquery-1.5.1.min.js"></script>
<script type="text/javascript">
 //test
 function test(uid)
 {
  if(confirm("确定该用户操作"+uid+"吗?"))
  {
  var date = new Date();
  var param = {uid:uid};
  jQuery.get("test.jsp?para=" + date, param,
    function(data){
     if(data != null)
     {
    var result = data.result;
    //alert("data==="+data);
    if(result == 1)
    {
    alert("操作失败");
    }
    if(result == 0)
    {
    alert("操作失败。");
    //document.location.href = document.location;
    //window.location.reload();
    }
     }
  });
  }
 }
</script>
<a href="javascript:void(0);" onclick="test(111111111111111111);">submit</a>

后台:

test.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%
response.setContentType("application/x-json;charset=UTF-8");
String uid=request.getParameter("uid");
int ret=0;
String result = "{\"result\":" + ret + "}";
out.print(result);
%>

注意:返回json格式 如{“ result”:0}

需要设置:

response.setContentType("application/x-json;charset=UTF-8");

jsp + jquery + ajax + json

Person,Address 2个类描述一个人,用ajax把数据取回显示在页面上,在DemoServlet里组装数据,项目用到的文件

  • json-lib-2.1-jkd15.jar
  • commons-lang-2.1.jar
  • commons-logging.jar
  • commons-collections-3.2.jar
  • commons-beanutils.jar
  • jquery.js
  • json.js

下面是详细代码:

人:

public class Person implements java.io.Serializable{   
    private String name;   
    private String sex;   
    private Integer age;   
    private Address address;   
    public String getName() {   
        return name;   
    }   
    public void setName(String name) {   
        this.name = name;   
    }   
    public String getSex() {   
        return sex;   
    }   
    public void setSex(String sex) {   
        this.sex = sex;   
    }   
    public Integer getAge() {   
        return age;   
    }   
    public void setAge(Integer age) {   
        this.age = age;   
    }   
    public Address getAddress() {   
        return address;   
    }   
    public void setAddress(Address address) {   
        this.address = address;   
    }   
}

地址:

public class Address implements java.io.Serializable{   
    private String province;   
    private String city;   
    private String country;   
    public Address() {   
    }   
    public Address(String province, String city, String country){   
        this.province =province;   
        this.city = city;   
        this.country = country;   
    }   
    public String getProvince() {   
        return province;   
    }   
    public void setProvince(String province) {   
        this.province =province;   
    }   
    public String getCity() {   
        return city;   
    }   
    public void setCity(String city) {   
        this.city = city;   
    }   
    public String getCountry() {   
        return country;   
    }   
    public void setCountry(String country) {   
        this.country = country;   
    }   
}

DemoServlet:

import java.io.IOException;   
import java.util.ArrayList;   
import java.util.List;   
import javax.servlet.ServletException;   
import javax.servlet.http.HttpServlet;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
import net.sf.json.JSONArray;   
import net.sf.json.JSONObject;   
import org.wncnke.json.pojo.Address;   
import org.wncnke.json.pojo.Person;   
public class DemoServlet extends HttpServlet{   
    @Override 
    protected void doGet(HttpServletRequest request,HttpServletResponse response)   
            throwsServletException, IOException {   
        doPost(request, response);  
    }   
    @Override 
    protected void doPost(HttpServletRequest request,HttpServletResponse response)   
            throwsServletException, IOException {   
       response.setContentType("text/xml;charset=utf-8");   
        response.setHeader("Cache-Control","no-cache");   

        try{   

            Personperson1 = new Person();   
           person1.setName("小王");  
           person1.setSex("女");   
           person1.setAge(23);   
            person1.setAddress(newAddress("辽宁省","大连市","高新园区"));   

           List<Person> list = new ArrayList<Person>();   
           list.add(person1);   

            try{   
               //取集合   
               JSONArray jsonArray = JSONArray.fromObject(list);   

               JSONObject jsonObject = new JSONObject();   

               jsonObject.put("person", jsonArray);   

               response.getWriter().write(jsonObject.toString());    

            } catch(IOException e) {   
               e.printStackTrace();   
           }   

        }catch(Exception e){   
           e.printStackTrace();   
        }   
    }   
}

web.xml:

<?xml version="1.0"encoding="UTF-8"?> 
<web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="http://caucho.com/ns/resin/core"> 

    <display-name>json</display-name> 

    <welcome-file-list> 
       <welcome-file>index.jsp</welcome-file>        
    </welcome-file-list> 

    <servlet> 
       <servlet-name>DemoServlet</servlet-name> 
       <servlet-class>org.wncnke.json.servlet.DemoServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
       <servlet-name>DemoServlet</servlet-name> 
       <url-pattern>/demoServlet</url-pattern> 
    </servlet-mapping> 

</web-app>

index.jsp:

view plaincopy to clipboardprint?

<%@ page language="java" 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%>"> 
<mce:script language="javascript" src="script/jquery.js"mce_src="script/jquery.js"></mce:script>     
<mce:script language="javascript" src="script/json.js"mce_src="script/json.js"></mce:script>    
<mce:script language="javascript"><!--   

function addPerson(){      
    $.getJSON("demoServlet",null,function call(data){   
        var list = data.person;   
        $.each(list, function(i, p){   
            var row =$("#tr").clone();   
           row.find("#name").text(p.name);   
           row.find("#age").text(p.age);   
           row.find("#sex").text(p.sex);   
           row.find("#address").text(p.address.province + p.address.city +p.address.country);   
           row.appendTo("#tbody");   
        });      
    });   
}      
// --></mce:script> 
</head> 
<body> 
<input type="button" value="JsonView"onClick="addPerson();">      
<div id="dateMessage">      
    <table id="planTable"border="1">      
        <tr> 
           <td>Name</td> 
           <td>Sex</td> 
           <td>Age</td> 
           <td>Address</td> 
       </tr>      
        <tbody id="tbody"> 
            <trid="tr"> 
               <td id="name"></td> 
               <td id="sex"></td> 
               <td id="age"></td> 
               <td id="address"></td> 
           </tr>      
        </tbody> 
    </table>      
</div> 
</body> 
</html>
由JSRUN为你提供的JSON在线运行、在线编译工具
        JSRUN提供的JSON 在线运行,JSON 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout