json example

Define user object
  1. function showJSON() {    
  2.     var user =     
  3.     {     
  4.         "username":"andy",    
  5.         "age":20,    
  6.         "info": { "tel""123456""cellphone""98765"},    
  7.         "address":    
  8.             [    
  9.                 {"city":"beijing","postcode":"222333"},    
  10.                 {"city":"newyork","postcode":"555666"}    
  11.             ]    
  12.     }    
  13.         
  14.     alert(user.username);    
  15.     alert(user.age);    
  16.     alert(user.info.cellphone);    
  17.     alert(user.address[0].city);    
  18.     alert(user.address[0].postcode);    
  19. }    

follow example  , encapsulate bean to json

 

 

 

public class Users  implements java.io.Serializable {
    
// Fields    
     private String userAccount;
     
private String userPwd;

    
// Constructors
    public Users() {
    }

    public Users(String userAccount, String userPwd) {
        
this.userAccount = userAccount;
        
this.userPwd = userPwd;
    }

   
    
// Property accessors
    public String getUserAccount() {
        
return this.userAccount;
    }

    
    
public void setUserAccount(String userAccount) {
        
this.userAccount = userAccount;
    }


    
public String getUserPwd() {
        
return this.userPwd;
    }

    
    
public void setUserPwd(String userPwd) {
        
this.userPwd = userPwd;
    }

   }

 



public class MMM {

    
public static void main(String[] args) {
        Users u 
= new Users("张三","123456");
        JSONObject jo 
= new JSONObject(u);
        System.out.println(jo.toString());
        
try {
            System.out.println(jo.get(
"userPwd"));
        }
 catch (JSONException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

 

你可能感兴趣的:(json,bean,object,String,user)