2013年8月23日星期五

java json parse

a. add flexjson-2.1.jar
b. create class

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
public class Users {
 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;
 }
 public static Users fromJsonToPostUser(String json) {
        return new JSONDeserializer<Users>().use(null, Users.class).deserialize(json);
    }

}


c. in main function

public class test {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stu
  String json = "{username:\"test1\",password:\"testp\"}";
  Users user = Users.fromJsonToPostUser(json);
  System.out.println(user.getUsername());
 }
}


URL ref: http://flexjson.sourceforge.net/javadoc/flexjson/JSONDeserializer.html

没有评论:

发表评论