最近工作中遇到了个问题。
代码如下:
代码如下:
resGet = httpClient.execute(method); String resData = EntityUtils.toString(resGet.getEntity(),"utf-8"); System.out.println(resData); //JSONObject JSONObject resJSON = JSONObject.fromObject(resData); System.out.println("resJSON转化resData是:"+resJSON); Object userjson = resJSON.get("user"); System.out.println("JSONObject转化的user对象是:"+userjson); String userString = userjson.toString(); //Gson Gson gson = new Gson(); String jsonString = gson.toJson(resData); System.out.println("jsonString转化resData是:"+jsonString); User user = gson.fromJson((String) userString, User.class); System.out.println("user.getName()是:"+user.getName()); /* *Gson gson = new Gson(); // Or use new GsonBuilder().create(); MyType target = new MyType(); String json = gson.toJson(target); // serializes target to Json MyType target2 = gson.fromJson(json, MyType.class); */ //JsonObject JsonObject jsonObj = new JsonObject(); jsonObj.getAsJsonObject(resData); System.out.println("jsonObj转化resData是:"+jsonObj); /*if(null != resJson.get("user")){ System.out.println("user在tu-login 是null"); User user = (User)resJson.get("user"); request.getSession().setAttribute("user", user); }*/ //JsonNull判断空值 if("A00000".equals(resJSON.get("code"))){ String token = (String)resJSON.get("token"); securityUtil.addCookieToken(request,response,token,user.getId()); return; }
想把得到的json字符串解析成一个USER对象。但是,关于这三者的区别,不知道怎么弄,谁能教教本人?
解决方案