/** * spirngcloud用这个RestTemplate来得到json对象 * @Autowired * private RestTemplate restTemplate; * * @Test * public void httpGet() { * com.wchulian.entity.UserBean user = this.restTemplate.getForObject("http://localhost:8088/hello/1", com.wchulian.entity.UserBean.class); * System.out.println(user); * } * * */ CloseableHttpClient httpClient; @Before public void init() { httpClient = HttpClients.createDefault(); } /* * 将json字符转成JSONObject对象 直接用对象.get就可以得到这个值 * */ @Test public void testGet() throws IOException, JSONException { HttpGet request = new HttpGet("http://121.205.88.117:6633/yunqp.php?url=RongXingVR-81115643039022"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); JSONObject a = new JSONObject(response); String url = a.get("url").toString(); System.out.println(url); } @Test public void testPost() throws IOException { HttpGet request = new HttpGet("http://www.oschina.net/"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); } @Test public void testGetPojo() throws IOException { HttpGet request = new HttpGet("http://localhost:8115/service/2"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); try { JSONObject object = new JSONObject(response); String password = object.get("password").toString(); System.out.println(password); } catch (JSONException e) { e.printStackTrace(); } } // HttpClient请求数据后是json字符串,需要我们自己把Json字符串反序列化为对象,我们会使用JacksonJson工具来实现。 // `JacksonJson`是SpringMVC内置的json处理工具,其中有一个`ObjectMapper`类,可以方便的实现对json的处理: // json处理工具 private ObjectMapper mapper = new ObjectMapper(); @Test public void testJson() throws IOException { UserBean user = new UserBean(); user.setId(8); user.setAge(21); user.setName("柳岩"); user.setUsername("liuyan"); // 序列化 将对象转成字符串 String json = mapper.writeValueAsString(user); System.out.println("json = " + json); // 序列化 // 反序列化,接收两个参数:json数据,反序列化的目标类字节码 UserBean result = mapper.readValue(json, UserBean.class); System.out.println(result.toString()); } // json处理工具 private ObjectMapper mapper1 = new ObjectMapper(); @Test public void testJson01() throws IOException { UserBean user = new UserBean(); user.setId(8); user.setAge(21); user.setName("柳岩"); user.setUsername("liuyan"); // 序列化,得到对象集合的json字符串 String json = mapper1.writeValueAsString(Arrays.asList(user, user)); // 反序列化,接收两个参数:json数据,反序列化的目标类字节码 List<UserBean> users = mapper1.readValue(json, new TypeReference<List<UserBean>>(){}); System.out.println(users.size()); for (UserBean u : users) { System.out.println("u = " + u); } } // json处理工具 private ObjectMapper mapper2 = new ObjectMapper(); @Test public void testJson02() throws IOException { UserBean user = new UserBean(); user.setId(8); user.setAge(21); user.setName("柳岩"); user.setUsername("liuyan"); // 序列化,得到对象集合的json字符串 String json = mapper2.writeValueAsString(Arrays.asList(user, user)); // 反序列化,接收两个参数:json数据,反序列化的目标类字节码 List<UserBean> users = mapper2.readValue(json, mapper2.getTypeFactory().constructCollectionType(List.class, UserBean.class)); System.out.println(users.size()); for (UserBean u : users) { System.out.println("u = " + u); } }
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!
常见资源合集和破解beqptwpmc...