原代码如下:
1 DefaultHttpClient httpClient = new DefaultHttpClient(); 2 HttpPost method = new HttpPost("http://localhost:8080/tu-login/tuloginMethod"); 3 4 JSONObject jsonParam = new JSONObject(); 5 jsonParam.put("phone", phone); 6 jsonParam.put("password", password); 7 jsonParam.put("requrl", requrl); 8 9 StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8"); 10 entity.setContentEncoding("UTF-8"); 11 entity.setContentType("application/json"); 12 method.setEntity(entity); 13 14 HttpResponse resGet; 15 try { 16 17 resGet = httpClient.execute(method); 18 String resData = EntityUtils.toString(resGet.getEntity(),"utf-8"); 19 System.out.println(resData); 20 21 //JSONObject 22 JSONObject resJSON = JSONObject.fromObject(resData); 23 System.out.println("resJSON转化resData是:"+resJSON); 24 Object userjson = resJSON.get("user"); 25 System.out.println("JSONObject转化的user对象是:"+userjson); 26 String userString = userjson.toString(); 27 //Gson 28 Gson gson = new Gson(); 29 String jsonString = gson.toJson(resData); 30 System.out.println("jsonString转化resData是:"+jsonString); 31 User user = gson.fromJson((String) userString, User.class); 32 System.out.println("user.getName()是:"+user.getName()); 33 /* 34 *Gson gson = new Gson(); // Or use new GsonBuilder().create(); 35 MyType target = new MyType(); 36 String json = gson.toJson(target); // serializes target to Json 37 MyType target2 = gson.fromJson(json, MyType.class); 38 */ 39 40 41 //JsonObject 42 JsonObject jsonObj = new JsonObject(); 43 jsonObj.getAsJsonObject(resData); 44 System.out.println("jsonObj转化resData是:"+jsonObj); 45 46 47 48 49 /*if(null != resJson.get("user")){ 50 System.out.println("user在tu-login 是null"); 51 User user = (User)resJson.get("user"); 52 request.getSession().setAttribute("user", user); 53 }*/ 54 //JsonNull判断空值 55 if("A00000".equals(resJSON.get("code"))){ 56 String token = (String)resJSON.get("token"); 57 securityUtil.addCookieToken(request,response,token,user.getId()); 58 } 59 60 //cookie --- token,requrl 61 62 //securityUtil.addCookie(request,response,requrl); 63 64 if ( !"A00000".equals(resJSON.get("code"))) { 65 result = resJSON.get("data"); 66 }else{ 67 result = securityUtil.getReqURL(request,response); 68 } 69 70 request.getSession().setAttribute("", ""); 71 resultCode = resJSON.get("code");
需求,解析返回的json字符串,把json转换成java对象。
在网上看到了很多,都是写死的,先从json中获取,然后再用set方法,放到对象中,我感觉这种方法不太好,就写了这种方法,参考了很多资料,谢谢各位大大。
json如下:
{"code":"A00000","data":null,"user":{"id":9,"name":"隔壁老王","email":"123","password":"4297F44B13955235245B2497399D7A93","deleted":false,"birthTime":1436371200000,"createTime":2015,"phone":"135135135","address":"123","perms":"cmsMng,cmsEdit,fileServerMng,fileServerAppMng,","tel":"123","permsName":null,"gender":0,"sex":"1","birth":null},"token":"f909d45e-1ff9-4f69-9cc0-503e5dde3025"}
code是返回的状态码,data是信息,user则是另一个json字符串,需要把它解析成user对象。token是需要保存的信息。
我这里,解析json用的是这个。net.sf.json.JSONObject,来解析json字符串。
并获得其中的user值。
Object userjson = resJSON.get("user"); System.out.println("JSONObject转化的user对象是:"+userjson); String userString = userjson.toString();
输出:
JSONObject转化的user对象是:{"id":9,"name":"隔壁老王","email":"123","password":"4297F44B13955235245B2497399D7A93","deleted":false,"birthTime":1436371200000,"createTime":2015,"phone":"135135135","address":"123","perms":"cmsMng,cmsEdit,fileServerMng,fileServerAppMng,","tel":"123","permsName":null,"gender":0,"sex":"1","birth":null}
然后再用:com.google.gson.Gson来将json转成user对象。
Gson gson = new Gson(); User user = gson.fromJson((String) userString, User.class);
这样,就可以了。
但是我有点地方不太明白。
我在用:com.google.gson.JsonObject这个的时候,却不能解析到值:
JsonObject jsonObj = new JsonObject(); jsonObj.getAsJsonObject(resData); System.out.println("jsonObj转化resData是:"+jsonObj);
jsonObj转化resData是:{}
请各位过路的大大指教。
同求,GSON,JSONObject,JsonObject,这三者的区别,我QQ:1286238812,欢迎各位高手骚扰。