欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json

程序员文章站 2022-05-31 10:23:52
...

一、json传输List

1.需要包:json-lib-0.9.jar     ezmorph-1.0.1.jar        (也许还有其他的)   

               json2.js

2.java代码

  

实体类:
public class TestJSONDO implements java.io.Serializable{
	private String name;
	private String password;
	private int age;
action中:
                               List list = new ArrayList();
		TestJSONDO t1 = new TestJSONDO("q1","p1",1);
		list.add(t1);
		TestJSONDO t2 = new TestJSONDO("q2","p2",2);
		list.add(t2);
		TestJSONDO t3 = new TestJSONDO("q3","p3",3);
                                 ......
                              JSONArray arr = JSONArray.fromObject(list);
	              pw.print(arr.toString());//写出去

 

 

3.js代码

function areaDiv(data){
	var areaDiv2 = document.getElementById("areaDiv2");
	for(var one in data){
		var namevalue = data[one]['name'];
		var agevalue = data[one]['age'];
	}
}
//查找区域
function findArea(){
	var provinces = document.getElementById("provinces");
	var urban = document.getElementById("urban");
	provinces = provinces.options[provinces.selectedIndex].value;
	urban = urban.options[urban.selectedIndex].value;
	$.post("test!hello.action", 
			{ 'provinces': provinces, 'urban': urban },		
			function(data){
				areaDiv(data);
			},"json");//必须要写上json,不然无法遍历
}

 

二。json格式数据转java对象(C\S)

    服务器:

  

package com.ajax;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import net.sf.json.JSONObject;

import org.apache.log4j.net.SocketServer;

public class JsonObjectServer {
	public static void main(String []a) throws IOException{
		ServerSocket ss = new ServerSocket(8888);//开启服务
		while (true) {
			Socket s = ss.accept();//等待
			BufferedReader buf = new BufferedReader(new InputStreamReader(s.getInputStream()));//输入流
			String message = buf.readLine();//读取json格式的数据
			System.out.println("string:"+message);//浏览格式
			JSONObject job = JSONObject.fromString(message);//将json格式的数据转换成json对象
			JsonObjectTest jot = (JsonObjectTest)JSONObject.toBean(job,JsonObjectTest.class);//将json对象赋值给对应测试类
			System.out.println("JsonObjectTest:"+jot.getName()+"  "+jot.getAge()+"  "+jot.getId()+"   "+jot.getPassword());
		}
		
	}
}

 

客服端:

  

package com.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import net.sf.json.JSONObject;

public class JsonObjectClient {
	public static void main(String []a) throws IOException{
		Socket s = new Socket("192.168.102.33",8888);//连接服务器
		PrintWriter pw = new PrintWriter(s.getOutputStream());//获取输出流
		JsonObjectTest jobt = new JsonObjectTest("hello",18);//被测试类
		JSONObject job = JSONObject.fromBean(jobt);//转成json对象
		pw.write(job.toString());//写出
		pw.flush();
		pw.close();
		s.close();
		System.out.println("ok");
	}
}

 

测试类:

   

package com.ajax;

public class JsonObjectTest {
	private int id;
	private String name;
	private String password;
	private int age;
	public JsonObjectTest(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public JsonObjectTest() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}