java后台怎样传json格式的数据
通过 JSONObject类就可以了首先 你把这几个包 下下来 放到你项目。如果有就不要下了:
1.commons-lang.jar2.commons-beanutils.jar3.commons-collections.jar4.commons-logging.jar 5.ezmorph.jar6.json-lib-2.
2.2-jdk15.jar像你这种是数据形式 就通过 JSONArray 如:JSONArray datasJson = JSONArray.fromObject(datas);最好把datas toString 一下
java写一个用于接收json数据的接口
java中的接口是一种特殊的类,使用关键字interface创建。接口功能完全实现后,可以打成jar包,提供给其他公司使用。要返回json格式数据,可以把接口中抽象方法的返回值类型规定为JSONObject或JSONString类型。这样当其他公司调用时,得到的数据就是json数据了。另外,以jar形式提供的接口,可以通过反编译得到你的源码,如果你不希望开源,就要加密了。
java在后台如何将前台传过来的json格式数据转换为map
我们需要先把json字符串转化为
net.sf.json.JSONObject
对象,java中这样就可以完成json字符串到Map的转换了。1.将数组转换为JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.对象转换成JSON:UserInfo user = new UserInfo(1001,"张三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map转换成json, 要使用jsonObject对象:Map<String, Object> map = new HashMap<String, Object>();map.put("userId", 1001);map.put("userName", "张三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);
你找一个解析json的包,Gson 或者 fastjson ,把你收到的那个字符串 转换为 JsonObject对象,然后你用Map的操作方式来操作JsonObject就行了。顺便说下,json的格式比较复杂,它比Map的格式要复杂些,所以,你不可能把json完全转换为Map,如果格式是按你说的{"a":0,"b":1,"c":1,"d":1} 格式是固定的话,那你自己把JsonObject转换成Map就可以了
还没有评论,来说两句吧...