String 转 Json,应用jar包com.google.gson

来源:互联网 发布:mac怎么创建文件夹 编辑:程序博客网 时间:2024/05/16 05:27

//result 必须是json格式的字符串

String result = "{\"address\":\"广东省.东莞市.东莞市.八达路.离金贸旅店约99米.离沁园宾馆(西北)约106米\",\"msg\":\"\",\"ret\":0}";

JsonObject jo = new JsonObject();//实例化

JsonParser jp = new JsonParser();

if(result != null){//转换成json时,result必须非空

try{

jo = jp.parse(result).getAsJsonObject();//result 转化成JsonObject
boolean = jo.has("ret");//判断转成json是否有ret这个属性

} catch(JsonParseException e){//谷米返回结果不是json格式

System.out.println("result非json格式");

}

} else {

System.out.println("result为空");

}