json 與 java 對象 之間的資料交換

來源:互聯網
上載者:User

// 推薦使用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]);
}
}
}
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.