本文目錄一覽:
怎麼把json返回的數據顯示到html頁面上?
需要準備的材料分別是:電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器,新建一個html文件,例如:index.html,並引入jquery.js。
2、在index.html的script標籤中,輸入js代碼:
$.get(‘請求地址’, function(respond) {
document.body.innerText = respond;
});
3、從瀏覽器運行index.html頁面,此時接口返回的json數據被完整顯示到html頁面上了。
如何在jsp頁面中顯示json
在jsp頁面中顯示json的方法是直接用out方法打印。
1、後台組裝json數據格式:
JsonObject jsonObject=new JsonObject();
jsonObject.addProperty(“name”, “john”);
jsonObject.addProperty(“age”, “18”);
jsonObject.addProperty(“sex”, “Male”);
HttpSession session=request.getSession(true);
session.setAttribute(“jsonObject”, jsonObject);
RequestDispatcher rd = request.getRequestDispatcher(“viewpage.jsp”);
rd.forward(request, response);
2、jsp頁面展示對應的json:
%@page import=”com.google.gson.JsonObject”%
%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″
titleView Json/title
%
JsonObject jsonObject=(JsonObject)session.getAttribute(“jsonObject”);
%
/head
body
h6JSON View/h6
br
%=jsonObject%
/body
/html
3、展示結果:
前端頁面顯示帶格式的json數據
首先後台返回數據格式形如下面:
var jsonData = ‘{↵↵ “reason”: “成功”,↵↵ “result”: {↵↵ “realname”: “董**”,/*真實姓名*/↵↵ “idcard”: “330329199******12”,/*身份證號碼*/↵↵ “res”: 1 /*1:匹配 2:不匹配*/↵↵ },↵↵ “error_code”: 0↵↵ }’;
然後JS處理如下:
jsonData = jsonData .replace(/↵/g,””).replace(/ /g,” “);
或者下面這種
jsonData = jsonData .replace(/\n/g,””).replace(/ /g,” “);
頁面上如果是框架的話 使用v-html 或者ng-html等,否則使用pre/pre也可以。
如何獲取網頁中的json數據?
1、首先打開你編寫網頁程序的軟件。
2、然後新建一個php文件,用於存放json數據。
3、接着在你新建的php文件填寫如下的代碼。
4、然後新建一個html文件。
5、接着就是書寫html的基本框架。
6、然後引入重要的JQuery文件。
7、接着書寫一個div標籤,用於存放json數據。
8、最後編寫js程序,來獲取html中的json數據。
原創文章,作者:MNIS,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/139892.html