Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

17.5. Json 內容展示

Struts 配置檔案

		
	<package name="information" extends="main" namespace="/inf">
		<action name="Information" class="com.example.action.Infomation">
			<result type="tiles">information</result>
		</action>
	</package>
		
		

Action 檔案

		
package cn.netkiller.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class Infomation extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	private JsonObject jsonObject = null;
	private String jsonString = "";

	@Override
	public String execute() throws IOException {
		
		String URL = "http://inf.example.com/list/json/93/20/0.html";
		System.out.printf("%s Requeted URL is %s", this.getClass().getName(), URL);
		
		StringBuilder sb = new StringBuilder();
		URLConnection urlConn = null;
		InputStreamReader in = null;
		try {
			URL url = new URL(URL);
			urlConn = url.openConnection();
			if (urlConn != null)
				urlConn.setReadTimeout(60 * 1000);
			if (urlConn != null && urlConn.getInputStream() != null) {
				in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
				BufferedReader bufferedReader = new BufferedReader(in);
				if (bufferedReader != null) {
					int cp;
					while ((cp = bufferedReader.read()) != -1) {
						sb.append((char) cp);
					}
					bufferedReader.close();
				}
			}
			in.close();

			jsonString = sb.toString();

			System.out.println(jsonString);

			JsonReader reader = Json.createReader(new StringReader(jsonString));

			JsonObject jsonObject = reader.readObject();
			this.setJsonObject(jsonObject);

			reader.close();

			// System.out.println(jsonObject.size());

			/*for (int i = 0; i < jsonObject.size() - 2; i++) {
				JsonObject rowObject = jsonObject.getJsonObject(Integer.toString(i));
				// System.out.println(rowObject.toString());
				System.out.printf("%s\t%s\t%s\n", rowObject.getJsonString("id"), rowObject.getJsonString("title"),
						rowObject.getJsonString("ctime"));
			}*/
			
			

		} catch (Exception e) {
			throw new RuntimeException("Exception while calling URL:" + URL, e);
		}

		return Action.SUCCESS;
	}

	public JsonObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
}
		
		

JSP 檔案

		
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<table>
	<c:forEach items="${jsonObject.entrySet()}" var="json"
		varStatus="status">
		<c:if test="${not status.last}">
			<tr>
				<td>${json.key+1}</td>
				<td>${json.value.getJsonString("id")}</td>
				<td>${json.value.getJsonString("title")}</td>
				<td>${json.value.getJsonString("ctime")}</td>
			</tr>
		</c:if>
	</c:forEach>
</table>
===================

<c:forEach items="${jsonObject.entrySet()}" var="json">
      ${json.value.toString()}
</c:forEach>

===========

<s:property value="jsonString" />
		
		

解決雙引號問題

		
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<table>
	<c:forEach items="${jsonObject.entrySet()}" var="json"
		varStatus="status">
		<c:if test="${not status.last}">

			<c:set var="id"
				value="${fn:replace(json.value.getJsonString('id'),'\"', '')}" />
			<c:set var="title" value="${fn:replace(json.value.getJsonString('title'),'\"', '')}" />
			<c:set var="ctime"
				value="${fn:replace(json.value.getJsonString('ctime'),'\"', '')}" />
			<tr>
				<td><c:out value="${status.count}" /></td>
				<td><a href="/inf/Detail.do?id=<c:out value="${id}"/>"><c:out
							value="${title}" /></a></td>
				<td><c:out value="${ctime}" /></td>
			</tr>
		</c:if>
	</c:forEach>
</table>

<c:set var="pages" value="${jsonObject.getJsonObject('pages')}" />


<a href="${pages.first}">首頁</a>
<a href="${pages.before}">上一頁</a>
<a href="${pages.next}">下一頁</a>
<a href="${pages.last}">尾頁</a>

<span>Count: ${pages.count}, Total: ${pages.total}</span>
		
		

17.5.1. 禁止方法

			
	@JSON(serialize = false)
	public String getDatas() {
		return datas;
	}
			

使用 excludeProperties 在 Action 中排除

			
	<action name="withdraw" class="cn.netkiller.api.action.Report" method="getHistory">
		<interceptor-ref name="defaultStack" />
		<interceptor-ref name="json">
			<param name="enableSMD">true</param>
		</interceptor-ref>
		<result name="success" type="json">
			<param name="enableGZIP">true</param>
			<param name="excludeProperties">.*direction</param>
		</result>
	</action>
			
			

17.5.2. 格式化日期

@JSON(format="yyyy-MM-dd") 
public Date getDate(){ 
	return date; 
}			
			

17.5.3. 重命名變數名

@JSON(name="mypassword") 
public String getPassword() { 
	return password; 
}
			

17.5.4. org.apache.struts2.json

JSONUtil.serialize(sb.toString());
JSONUtil.deserialize(sb.toString());