当前位置: 代码迷 >> JavaScript >> java中json的应用
  详细解决方案

java中json的应用

热度:167   发布时间:2012-09-14 23:00:49.0
java中json的使用

json串示例

[{"id":1,"name":"name1","password":"password1","remark":"remark1"},{"id":2,"name":"name2","password":"password2","remark":"remark2"},{"id":3,"name":"name3","password":"password3","remark":"remark3"}]

主要分为以下几种:

1. 直接构建json串,主要代码;

2. json串 -> java对象;

3. java对象 -> json串/json对象;

4. List<?> -> json串;

5. 对象数组 -> json串。

程序如下:

TestJson.java

import java.util.*;

import net.sf.json.*;

import com.hs.sig.ui.sgms.vo.User;

public class TestJson {

/**

* 初始化userList大小

*/

public static final int USERLISTSIZE = 3; 

/**

* 直接构建json串

* @return

*/

public String toJsonString() {

// 初始化 User 对象

User user = null;

List<User> userList = this.initUserList(USERLISTSIZE);

// 构建 json 串

StringBuffer json = new StringBuffer();

json.append("[");

for (int i = 0; i < userList.size(); i++) {

user = userList.get(i);

json.append("{");

json.append("\"id\":\"").append(user.getId()).append("\",");

json.append("\"name\":\"").append(user.getName()).append("\",");

json.append("\"password\":\"").append(user.getPassword()).append("\",");

json.append("\"remark\":\"").append(user.getRemark()).append('"');

json.append("},");

}

json.deleteCharAt(json.lastIndexOf(","));

json.append("]");

//System.out.println("直接构建 json 串结果:" + json.toString());

return json.toString();

}

/**

* json串 -> java对象

* @return

*/

public String jsonToObject() {

StringBuffer info = new StringBuffer();

if(this.isArray(this.toJsonString())){

JSONArray jSONArray = JSONArray.fromObject(this.toJsonString());

JsonConfig jsonConfig = new JsonConfig();

jsonConfig.setArrayMode(JsonConfig.MODE_LIST);

jsonConfig.setRootClass(User.class);

List<User> userList = (List<User>) JSONSerializer.toJava(jSONArray, jsonConfig);

User user = null;

for (int i = 0; i < userList.size(); i++) {

user = userList.get(i);

info.append("user" + (i + 1) + ".id=" + user.getId());

info.append(",user" + (i + 1) + ".name=" + user.getName());

info.append(",user" + (i + 1) + ".password=" + user.getPassword());

info.append(",user" + (i + 1) + ".remark=" + user.getRemark());

info.append("\n");

}

//System.out.println("json串 -> java对象:" + info.toString());

}

return info.toString();

}

/**

* java对象 -> json串/json对象

* @return

*/

public String objectToJson() {

// 初始化一个user对象

User user = null;

List<User> userList = this.initUserList(USERLISTSIZE);

user = userList.get(0);

// 转换user对象的配置

JsonConfig jsonConfig = new JsonConfig();

// 过滤对象中不需要转化的属性

jsonConfig.setExcludes(new String[]{"id"});

// 将对象user转化为json串

JSONArray jsonArray = JSONArray.fromObject(user, jsonConfig);

//jsonArray = JSONArray.fromObject(user);

// 转化为json对象

//JSONObject jsonObject = JSONObject.fromObject(user, jsonConfig);

//jsonObject = JSONObject.fromObject(user);

return jsonArray.toString();

}

/**

* List<User> -> json串

* @return

*/

public String listToJson(){

List<User> userList = this.initUserList(USERLISTSIZE);

// list转换为JSON串

JSONArray jsonArray = JSONArray.fromObject(userList);

//System.out.println("List<User> -> json串:" + jsonArray.toString());

return jsonArray.toString();

}

/**

* 对象数组 -> json串

* @return

*/

public String arrayToJson() {

List<User> userList = this.initUserList(USERLISTSIZE);

User[] userArray = new User[userList.size()];

for(int i = 0 ; i < userList.size() ; i++){

userArray[i] = userList.get(i);

}

// user数组转为json串

JSONArray jsonArray = JSONArray.fromObject(userArray);

//System.out.println("对象数组 -> json串:" + jsonArray.toString());

return jsonArray.toString();

}

/**

* 初始化用户

* @param userListSize list 的大小

* @return

*/

private List<User> initUserList(int userListSize) {

User user = null;

List<User> userList = new ArrayList<User>();

for (int i = 0; i < userListSize; i++) {

user = new User();

user.setId(i + 1);

user.setName("name" + (i + 1));

user.setPassword("password" + (i + 1));

user.setRemark("remark" + (i + 1));

userList.add(user);

}

return userList;

}

/**

* 判断JSON串是否以"["开头,是则认定为jsonArray,否则认定为jsonObject

* @param str

* @return

*/

private boolean isArray(String str) {

return str.startsWith("[") ? true : false;

}

public static void main(String[] args) {

TestJson testJson = new TestJson();

System.out.println("直接构建json串:" + testJson.toJsonString());

System.out.println("json串 -> java对象:" + testJson.jsonToObject());

System.out.println("java对象 -> json串:" + testJson.objectToJson());

System.out.println("List<User> -> json串:" + testJson.listToJson());

System.out.println("对象数组 -> json串:" + testJson.arrayToJson());

}

}

?User.java

?

public class User {

private Integer id;

private String name;

private String password;

private String remark;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getRemark() {

return remark;

}

public void setRemark(String remark) {

this.remark = remark;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

}

?

直接构建json串:[{"id":"1","name":"name1","password":"password1","remark":"remark1"},{"id":"2","name":"name2","password":"password2","remark":"remark2"},{"id":"3","name":"name3","password":"password3","remark":"remark3"}]

json串 -> java对象:user1.id=1,user1.name=name1,user1.password=password1,user1.remark=remark1

user2.id=2,user2.name=name2,user2.password=password2,user2.remark=remark2

user3.id=3,user3.name=name3,user3.password=password3,user3.remark=remark3

java对象 -> json串:[{"name":"name1","password":"password1","remark":"remark1"}]

List<User> -> json串:[{"id":1,"name":"name1","password":"password1","remark":"remark1"},{"id":2,"name":"name2","password":"password2","remark":"remark2"},{"id":3,"name":"name3","password":"password3","remark":"remark3"}]

对象数组 -> json串:[{"id":1,"name":"name1","password":"password1","remark":"remark1"},{"id":2,"name":"name2","password":"password2","remark":"remark2"},{"id":3,"name":"name3","password":"password3","remark":"remark3"}]

  相关解决方案