json 与 java 对象 之间的数据交换

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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值