- function showJSON() {
- var user =
- {
- "username":"andy",
- "age":20,
- "info": { "tel": "123456", "cellphone": "98765"},
- "address":
- [
- {"city":"beijing","postcode":"222333"},
- {"city":"newyork","postcode":"555666"}
- ]
- }
- alert(user.username);
- alert(user.age);
- alert(user.info.cellphone);
- alert(user.address[0].city);
- alert(user.address[0].postcode);
- }
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();
}
}
}