package Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
public class Jtest {
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("aa", "123aa");
hm.put("bb", "123bb");
hm.put("cc", "123cc");
User user = new User();
user.setUsername("hewei");
user.setPassword("123");
List ul = new ArrayList();
ul.add(user);
//JSONArray jsona = JSONArray.fromObject("['aa','bb','']");
//JSONObject jsono = JSONObject.fromObject(new User());
//json 转 实体类
JSONObject jsonobject = JSONObject.fromObject("{username:'xx',password:'yy'}");
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY );
jsonConfig.setRootClass(User.class);
User user2 = (User) JSONSerializer.toJava(jsonobject,jsonConfig);
System.out.println(user2.getUsername()+" "+user2.getPassword());
//实体类转 json
JSONObject jsonobject2 = JSONObject.fromObject(user);
System.out.println(jsonobject2.toString());
}
}
package Test;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}