各种格式之间转换

来源:互联网 发布:python 2.7 re.compile 编辑:程序博客网 时间:2024/04/27 15:07

String转json
String jstr=”{‘json’:’jsonvalue’,’bool’:true,’int’:1,’double’:’20.5’}”;
JSONObject json=JSONObject.fromObject(jstr);
boolean bool=json.getBoolean(“bool”);
int i=json.getInt(“int”);
double d=json.getDouble(“double”);
String value=json.getString(“json”);

String转对象

class User{
private String name;
private String psw;
//封装getter/setter省略
}
String u=”{‘name’:’sail331x’,’psw’:’123456789’}”;
User user=(User)JSONObject.toBean(JSONObject.fromObject(u),User.class);

对象转String
把一个user变成json对象:
JSONObject juser=JSONObject.fromObject(user);
String jstr=juser.toString();

0 0