Android-IphoneはHttpプロトコルを用いてJson形式でサーバ側とクライアント通信を行う

2180 ワード

テスト類pojoとjsonは太極を打つ


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
/**
 * JSON   pojo     
 * 
 *    ezmorph-1.0.2.jar
 * @author myth
 *
 */
public class JSONTestForStruts {

	/**
	 *     pojo(Plain Old Java Objects)   JSON   
	 * @return
	 */
	public  JSONObject bean2json() {
		User user = new User();
		user.setId("JSONTest");
		user.setName("JSONTest");
		user.setPassword("JSON");
		user.setSay("Hello,i am JSONTest.java");
		JSONObject jsonObject = new JSONObject();
		
		ArrayList<User> list=new ArrayList<User>();
		list.add(user);
		list.add(user);
		
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(220180, null);
		map.put(220181, "Json   ");
		
		jsonObject.accumulate("user", user);//accumulate  
		jsonObject.accumulate("list", list);
		jsonObject.accumulate("map", map);
		System.out.println("User   JSON      :"+jsonObject.toString());
		return jsonObject;
	}

	/**
	 *  JSONObject        User  
	 * @param jsonObject
	 */
	public  void json2bean(JSONObject jsonObject) {
		User user=(User)JSONObject.toBean((JSONObject)jsonObject.get("user"),User.class);
		System.out.println("     User   Name :"+user.getName());
	}

	public static void main(String[] s) {
		JSONTestForStruts jsonTest=new JSONTestForStruts();
		jsonTest.json2bean(jsonTest.bean2json());
	}
}
アクションで呼び出す
response.setContentType("text/html;charset=gbk");
		 PrintWriter out=response.getWriter();
		//            
		JSONObject json=new JSONObject();
		json.accumulate("success", true);
		json.accumulate("esff", esff);
		out.println(json.toString());
		out.flush();
		out.close();