标签:http ar os sp java on div bs ad
Java code
1
|
{ "weatherinfo" :{ "city" : "北京" , "cityid" : "101010100" , "temp1" : "-8℃" , "temp2" : "4℃" , "weather" : "晴" , "img1" : "n0.gif" , "img2" : "d0.gif" , "ptime" : "18:00" }} |
JSONObject jsonObj = (JSONObject)jParser.parse("楼主的字符串");
Object obj = jsonObj .get ("weatherinfo");
JSONObject subObj = (JSONObject)obj ;
String city= (String)subObj .get("city");
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");
System.out.println("bool="+String.valueOf(bool)+"\tjson="+value+"\tint="+i+"\tdouble="+d);
假如你是有一个bean对象
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);
就可以了。
把一个user变成json对象:
JSONObject juser=JSONObject.fromObject(user);
String jstr=juser.toString();//这个就变成json字符串了
标签:http ar os sp java on div bs ad
原文地址:http://www.cnblogs.com/sdj89/p/4118658.html