package com.alex.entity;
public class JobEntity {
private String loc;
private String deptName;
private Float Sal;
public JobEntity() {
super();
}
public JobEntity(String loc, String deptName, Float sal) {
super();
this.loc = loc;
this.deptName = deptName;
Sal = sal;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Float getSal() {
return Sal;
}
public void setSal(Float sal) {
Sal = sal;
}
}
package com.alex.entity;
import java.util.HashSet;
import java.util.Set;
public class UserinfoEntity {
private Integer id;
private String name;
private Set jobEntity = new HashSet();
public UserinfoEntity() {
super();
}
public UserinfoEntity(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public UserinfoEntity(Integer id, String name, Set jobEntity) {
super();
this.id = id;
this.name = name;
this.jobEntity = jobEntity;
}
public Set getJobEntity() {
return jobEntity;
}
public void setJobEntity(Set jobEntity) {
this.jobEntity = jobEntity;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.alex.json;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import com.alex.entity.JobEntity;
import com.alex.entity.UserinfoEntity;
public class JsonTest {
//["hello"] | ["hello","world"]
public void test1() {
JSONArray jsonArray = new JSONArray();
jsonArray.put("hello");
jsonArray.put("world");
System.out.println(jsonArray);
}
//{"two":"world","one":"hello"} key取名一样则会覆盖前者
public void test2() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("one", "hello");
jsonObject.put("two", "world");
System.out.println(jsonObject);
}
//[[1,"A1"],[2,"A2"],[3,"A3"],[4,"B1"],[5,"B2"]]
public void test3() {
ArrayList<UserinfoEntity> user = new ArrayList<UserinfoEntity>();
user.add(new UserinfoEntity(1, "A1"));
user.add(new UserinfoEntity(2, "A2"));
user.add(new UserinfoEntity(3, "A3"));
user.add(new UserinfoEntity(4, "B1"));
user.add(new UserinfoEntity(5, "B2"));
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < user.size(); i++) {
UserinfoEntity u = (UserinfoEntity) user.get(i);
JSONArray ja = new JSONArray();
ja.put(u.getId());
ja.put(u.getName());
jsonArray.put(ja);
}
System.out.println(jsonArray);
}
//[{"userId":1,"userName":"A1"},{"userId":2,"userName":"A2"},{"userId":3,"userName":"A3"},{"userId":4,"userName":"B1"},{"userId":5,"userName":"B2"}]
public void test4() {
ArrayList<UserinfoEntity> user = new ArrayList<UserinfoEntity>();
user.add(new UserinfoEntity(1, "A1"));
user.add(new UserinfoEntity(2, "A2"));
user.add(new UserinfoEntity(3, "A3"));
user.add(new UserinfoEntity(4, "B1"));
user.add(new UserinfoEntity(5, "B2"));
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < user.size(); i++) {
UserinfoEntity u = (UserinfoEntity) user.get(i);
JSONObject jo = new JSONObject();
jo.put("userId", u.getId());
jo.put("userName", u.getName());
jsonArray.put(jo);
}
System.out.println(jsonArray);
}
//{"job":[["\u79d8\u4e66","\u603b\u88c1\u529e",6000],["J2EE\u7a0b\u5e8f\u5458","\u7814\u53d1\u90e8",8777],["\u884c\u653f\u4e13\u5458","\u884c\u653f\u90e8",1500],["\u9500\u552e\u5458","\u9500\u552e\u90e8",2911]],"userinfo":[1,"\u5f20\u5c0f\u4e09"]}
public void test5() {
JobEntity job1 = new JobEntity("销售员", "销售部", 2911F);
JobEntity job2 = new JobEntity("秘书", "总裁办", 6000F);
JobEntity job3 = new JobEntity("J2EE程序员", "研发部", 8777F);
JobEntity job4 = new JobEntity("行政专员", "行政部", 1500F);
UserinfoEntity userEntity = new UserinfoEntity(1, "张小三");
userEntity.getJobEntity().add(job1);
userEntity.getJobEntity().add(job2);
userEntity.getJobEntity().add(job3);
userEntity.getJobEntity().add(job4);
//外层
JSONObject jsonObject = new JSONObject();
JSONObject jobObject = new JSONObject();
//里层
JSONArray ja= new JSONArray();
JSONArray jsonArray = new JSONArray();
//转换user
jsonArray.put(userEntity.getId());
jsonArray.put(userEntity.getName());
jsonObject.put("userinfo", jsonArray);
//转换job
Iterator it = userEntity.getJobEntity().iterator();
while(it.hasNext()) {
JobEntity job = (JobEntity)it.next();
JSONArray temp= new JSONArray();
temp.put(job.getLoc());
temp.put(job.getDeptName());
temp.put(job.getSal());
ja.put(temp);
}
jsonObject.put("job", ja);
System.out.println(jsonObject);
}
//{"2":["B1","B2"],"1":["A1","A2","A3"]}
public void test6() {
ArrayList<UserinfoEntity> user = new ArrayList<UserinfoEntity>();
user.add(new UserinfoEntity(1, "A1"));
user.add(new UserinfoEntity(1, "A2"));
user.add(new UserinfoEntity(1, "A3"));
user.add(new UserinfoEntity(2, "B1"));
user.add(new UserinfoEntity(2, "B2"));
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < user.size(); i++) {
UserinfoEntity u = (UserinfoEntity) user.get(i);
if (jsonObject.has(u.getId().toString())) {
JSONArray jsonArray = (JSONArray) jsonObject.get(u.getId()
.toString());
jsonArray.put(u.getName());
} else {
JSONArray jsonArray = new JSONArray();
jsonArray.put(u.getName());
jsonObject.put(u.getId().toString(), jsonArray);
}
}
System.out.println(jsonObject);
}
public static void main(String[] args) throws Exception {
new JsonTest().test1();
new JsonTest().test2();
new JsonTest().test3();
new JsonTest().test4();
new JsonTest().test5();
new JsonTest().test6();
}
}