java中object数据怎么转换成json数据
你可以通过这个(json-lib-2.3-jdk15.jar)jar里的方法转换
JSONObject json = JSONObject.fromObject(Object);
如果对象数组
JSONArray json = JSONArray .fromObject(person);
怎么把json对象转换为json字符串
用Gson转换就行了,需要下载jar包
例子:
Person person=new Person();Gson gson=new Gson();String json=gson.toJson(person);
如何把map转成json
把map转成json的步骤:
1、需要一些jar包:json-lib-2.4-jdk15.jar、ezmorph-1.0.6.jar、commons-logging.jar、commons-lang.jar、commons-collections.jar、commons-beanutils.jar。
2、把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);
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);



还没有评论,来说两句吧...