JAvaにおけるjsonの使用

9339 ワード

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("
"); } //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":"name 1","password":"password 1","remark":"remark 1"},{"id":"id":"2","name":"name 2","password":"password 2","password 2","remark":"remark":"remark 2"},{"id":"3","name":"name":"name":"name 3","password":"password 3","remark","remark":""remark":"remark 3"}]
json列->javaオブジェクト:user 1.id=1,user 1.name=name 1,user 1.password=password 1,user 1.remark=remark 1
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”:“name 1”,“password”:“password 1”,“remark”:“remark 1”}]
リスト<User>>>>>>json列:[{“id”:1,“name”:“name 1”,“password”:“password 1”,“remark”:“remark 1”},{“id”:2,“name”:“name 2”,“password”:“password 2”,“password d 2”,“remark”:“remark 2”,{“id”:3,“name”:“name 3”,“password”:“password 3”,“remark”:“remark”:“remark”:“remark”:“remark”:“remark”:“remark”:“3,“id”:“mark 3"}]
オブジェクト配列->json列:[{"id":1,"name":"name 1","password":"password 1","remark":"remark 1"},{"id":2,"name":"name 2","password":"password":"password 2","remark":"remark":"remark 2"},{"id":3,"name":"name 3","password":"password 3","remark":"remark":"remark":"remark":"remark":remark 3"},{id":"id":3,"name":"name":"}]