json 을 만 드 는 자바 클래스http://www.gold98.net 하 다.

3142 단어 자바json.netJ#
import java.util.ArrayList;
public class Json {
	public static void main(String[] args){
		Json j=new Json();
		j.reSet();
		j.setSuccess(true);
		j.addItem("id","1");
		j.addItem("name","zb");
		j.addItemOk();
		j.addItem("id","2");
		j.addItem("name", "jim");
		j.addItemOk();
		j.addItem("id","3");
		j.addItem("name", "lh_zn");
		j.addItemOk();
		String dd=j.ToString();
		System.out.println(dd);
		
	}

	public String singleInfo="";
	protected boolean _success=true;
	protected String _error="";
	protected ArrayList arrData=new ArrayList();
	protected ArrayList dataItem=new ArrayList();
	public String getError(){
		return _error;
	}
	public void setError(String error){
		if(!error.equals("")) this._success=false;
		this._error=error;
	}
	public boolean getSuccess(){
		return _success;
	}
	public void setSuccess(boolean success){
		if(success) this._error="";
		this._success=success;
	}
	public Json()
	{
		
	}
	public void reSet()
	{
		_success=true;
		_error="";
		singleInfo="";
		arrData.clear();
		dataItem.clear();
	}
	public void addItem(String name,String _value)
	{
		dataItem.add(name);
		dataItem.add(_value);
	}
	public void addItemOk()
	{
		arrData.add(dataItem);
		dataItem=new ArrayList();
	}
	public String ToString()
	{
		StringBuilder sb=new StringBuilder();
		sb.append("{");
		sb.append("success:"+_success+",");
		sb.append("error:\""+_error.replaceAll("\"", "\\\"")+"\",");
		sb.append("singleInfo:\""+singleInfo.replaceAll("\"","\\\"")+"\","+"<br>");
		sb.append("data:["+"<br>");
		int ad=arrData.size();
		for(int i=0;i<ad;i++)
		{
			ArrayList arr=(ArrayList)(arrData.get(i));
			sb.append("{");
			int t=arr.size();
			for(int j=0;j<t;j+=2)
			{
				if(j==t)break;
				sb.append(arr.get(j).toString());
				sb.append(":");
				sb.append("\"");
				sb.append(arr.get(j+1).toString());
				sb.append("\"");
				if(j<t-2)sb.append(",");
			}
			sb.append("}");
			if(i<ad-1) sb.append(","+"<br>");
		}
		sb.append("<br>]");
		sb.append("}");
		return sb.toString();
	}
}

 
<%@page import="java.util.List"%>
<%
    Json json=new Json();
    UserService user = new UserService();
    List list = user.getUserList();
    Users users;
    json.reSet();
    json.setSuccess(true);
    int j=list.size();
    for (int i=0; i<j; i++)
    {
        users = (Users) list.get(i);
        json.addItem("userId",users.getId().toString());
        json.addItem("name",users.getUserName());
        json.addItem("pwd",users.getUserPwd());
        json.addItem("age",users.getUserAge().toString());
        json.addItemOk();
    }
    out.println(json.ToString());
%>

좋은 웹페이지 즐겨찾기