码迷,mamicode.com
首页 > 编程语言 > 详细

curl javaSSm传入json数组的格式方法

时间:2017-03-20 15:23:36      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:object   cat   res   row   .post   exce   127.0.0.1   ica   ons   

 

curl与java结合传入get.post,head请求,

比如: curl的地址:

      curl -l 127.0.0.1:8080/spacobj/core/do?acid=100 -H "token:101hh" -H "Content-type: application/json" -X POST -d {"telNum":"13521389587","pass":"‘my12345‘","nick":"‘明天会更好‘","sms":"‘7864AQ‘"}

 注意 json格式中的字符串必须是"‘ 里面是字符串‘"

java中获取的代码如下:

@RequestMapping(value = "do", method = RequestMethod.POST)
public String mydo(
@RequestParam(value = "acid", required = false, defaultValue = "0") int acid,
@RequestHeader(value = "token", required = false, defaultValue = "0") String token,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException, IOException, ParseException {

  //下面创建一个对象,用对象来接受json传过来的数据

T_Account tac = new T_Account();
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(
request.getInputStream(), "UTF-8"));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);

String json = sb.toString();
JSONObject jsonObject = JSONObject.fromObject(json);

Iterator ite = jsonObject.keys();

// 遍历jsonObject数据,用对象来接收。通过key的判断,是那个对象的字段来接受post中json请求过来的

while (ite.hasNext()) {
String key = ite.next().toString();

if (key.equals("telNum")) {
tac.setTelNum(jsonObject.get(key).toString());
}
if (key.equals("pass")) {
tac.setPass(jsonObject.get(key).toString());
}
if (key.equals("nick")) {
tac.setNick(jsonObject.get(key).toString());
}
if (key.equals("sms")) {
tac.setSms(jsonObject.get(key).toString());
}

String value = jsonObject.get(key).toString();

}

}

curl javaSSm传入json数组的格式方法

标签:object   cat   res   row   .post   exce   127.0.0.1   ica   ons   

原文地址:http://www.cnblogs.com/dapengsoso/p/6589184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!