最近有些小伙伴不知道java如何接收json数据?那么今天我们就给大家分享一些这方面的知识,希望文章能够帮助到大家哦!
具体的内容如下:
1.java后台给指定接口发送json数据
package com.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import net.sf.json.JSONObject; public class testOne { public static void main(String[] args) { JSONObject jsobj1 = new JSONObject(); JSONObject jsobj2 = new JSONObject(); jsobj2.put("deviceID", "112"); jsobj2.put("channel", "channel"); jsobj2.put("state", "0"); jsobj1.put("item", jsobj2); jsobj1.put("requestCommand", "control"); post(jsobj1, "http://192.168.3.4:8080/HSDC/test/authentication"); } public static String post(JSONObject json, String path) { String result = ""; try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "appliction/json"); post.addHeader("Authorization", "Basic YWRtaW46"); StringEntity s = new StringEntity(json.toString(), "utf-8"); s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "appliction/json")); post.setEntity(s); HttpResponse httpResponse = client.execute(post); InputStream in = httpResponse.getEntity() .getContent(); BufferedReader br = new BufferedReader(new InputStreamReader( in , "utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { strber.append(line + "\n"); } in .close(); result = strber.toString(); if (httpResponse.getStatusLine() .getStatusCode() != HttpStatus.SC_OK) { result = "服务器异常"; } } catch (Exception e) { System.out.println("请求异常"); throw new RuntimeException(e); } System.out.println("result==" + result); return result; } }
2.java后台接收json数据
package com.controller; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping("test") public class TestConttroller { @Resource protected HttpServletRequest request; @RequestMapping(value = "authentication", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) public Map < String, Object > getString() throws UnsupportedEncodingException, IOException { System.out.println("进入====================="); //后台接收 InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8"); char[] buff = new char[1024]; int length = 0; while ((length = reader.read(buff)) != -1) { String x = new String(buff, 0, length); System.out.println(x); } //响应 Map < String, Object > jsonObject = new HashMap < String, Object > (); //创建Json对象 jsonObject.put("username", "张三"); //设置Json对象的属性 jsonObject.put("password", "123456"); return jsonObject; } }
运行testOne之后将json数据发送到authentication接口,接收的数据如图:
testOne中main方法返回的数据如图:
至此java后台发送及接收json数据代码也就完成了。
json的类型有哪些?
1.jsons数组类型
var json1=[
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName": "Jones" }
];
2.json对象类型
var json2= { "firstName":"John" , "lastName":"Doe" };
3..json对象数组
var json = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
Json 是一种轻量级的数据交换格式。它基于 javascript的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。它的优点:
1、简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。
2、易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
然后说json的常用类型:在js语言中,一切都是对象,因此任何支持的类型都可以通过json来表示。json支持的数据类型有:
1、两种特殊类型:数组(array)、对象(object)
2、四种基础类型:字符串(string)、数字(number)、布尔型(boolean)、NULL值
其中对象和数组是比较特殊且常用的两种类型。
好了,今天的分享到这里就结束了,大家可以参考以上的方法去用Java接收json数据,另外,关于json的一些数据类型大家也要知道哦,最后大家如果想要了解更多json相关知识,敬请关注奇Q工具网。
推荐阅读: