java解析json串
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字符串了
JSONObject jsonPics = new JSONObject(pics);
String pic = jsonPics.get("basicPic").toString();
if(pic!="") {
int index = pic.lastIndexOf(",");
if(index != -1) {
String[] picstring = pic.substring(1, pic.length()-2).split(",");
JSONArray detailPic = jsonPics.optJSONArray("detailPic");
detailPic.length()>0
detailPic.getLong(i)
浙公网安备 33010602011771号