// 推薦使用www.json.com/google-gson-2.1jar
package com.jason.servlet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class TestDemo
{
public static void main(String[] args)
{
String str = "{\"裝置銷售流程\":\"koId-001\",\"耗材銷售流程\":\"koId-002\"}";
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(str);// java解析 jason字串
JsonObject jo = je.getAsJsonObject();
Set<Entry<String, JsonElement>> set = jo.entrySet();
for (Iterator<Entry<String, JsonElement>> iter = set.iterator(); iter
.hasNext();)
{
Entry<String, JsonElement> e = iter.next();
System.out.println(e.getKey() + ", " + e.getValue());
}
Gson gson = new Gson();
// 操作java對象
WorkToDo w1 = new WorkToDo();
w1.setId("流程ID-001");
w1.getItemMap().put("作業ID-001", "啟動");
w1.getItemMap().put("作業ID-002", "結束");
WorkToDo w2 = new WorkToDo();
w2.setId("流程ID-002");
w2.getItemMap().put("作業ID-001", "啟動");
w2.getItemMap().put("作業ID-002", "結束");
Process p = new Process();
p.setProName("裝置銷售流程");
p.setKoId("koId-001");
List<WorkToDo> ws = new ArrayList<WorkToDo>();
ws.add(w1);
ws.add(w2);
p.setWorkToDoList(ws);
int count = 0;
for (WorkToDo w : ws)
{
count += w.getCount();
}
p.setCount(count);
System.out.println(gson.toJson(p));
}
}
class Process
{
private String proName;
private String koId;
private int count;
private List<WorkToDo> workToDoList = new ArrayList<WorkToDo>();
public Process()
{
}
public Process(String koId, List<WorkToDo> workToDoList)
{
this.koId = koId;
this.workToDoList = workToDoList;
}
public String getKoId()
{
return koId;
}
public void setKoId(String koId)
{
this.koId = koId;
}
public List<WorkToDo> getWorkToDoList()
{
return workToDoList;
}
public void setWorkToDoList(List<WorkToDo> workToDoList)
{
this.workToDoList = workToDoList;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
public String getProName()
{
return proName;
}
public void setProName(String proName)
{
this.proName = proName;
}
}
class WorkToDo
{
private String id;
private Map<String, String> itemMap = new HashMap<String, String>();
public WorkToDo()
{
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public Map<String, String> getItemMap()
{
return itemMap;
}
public void setItemMap(Map<String, String> itemMap)
{
this.itemMap = itemMap;
}
public int getCount()
{
return this.itemMap.size();
}
}
輸出:裝置銷售流程, "koId-001"
耗材銷售流程, "koId-002"
{"proName":"裝置銷售流程","koId":"koId-001","count":4,"workToDoList":[{"id":"流程ID-001","itemMap":{"作業ID-002":"結束","作業ID-001":"啟動"}},{"id":"流程ID-002","itemMap":{"作業ID-002":"結束","作業ID-001":"啟動"}}]}
//ajax 資料發送
var xmlHttpRequest;
function ajaxTest()
{
if (window.ActiveXObject)
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
//xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP");//適合IE新版本,不過上面的也可以使用
else if (window.XMLHttpRequest)
xmlHttpRequest = new XMLHttpRequest();
if (xmlHttpRequest != "undefined")
//if ("undefined" != xmlHttpRequest)// 切忌此種寫法不要推薦,有可能在IE不正確
{
// 準備好資料,get請求;url=AjaxServlet; true 表示非同步處理
var url = "${pageContext.request.contextPath}/AjaxServlet";
// GET方式提交,參數跟在url
/*xmlHttpRequest.open("GET", url, true);
xmlHttpRequest.onreadystatechange = ajaxCallback; // 回呼函數
xmlHttpRequest.send(null); // 發送請求
*/
//POST方式提交
xmlHttpRequest.open("POST", url, true);
xmlHttpRequest.onreadystatechange = ajaxCallback; // 回呼函數
var json = {"001":"a", "002":"b"};
xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");// 必須增加
xmlHttpRequest.send("json=" + JSON.stringify(json)); // 發送請求
}
}
function ajaxCallback()
{
if (xmlHttpRequest.readyState == 4)
{
if (xmlHttpRequest.status == 200)
{
var json = eval('('+xmlHttpRequest.responseText+')');
for(var i in json)
{
alert(i + ", " + json[i]);
}
}
}
}