java中json的使用
2016-07-13 13:20
573 查看
String s = "src/text/multipleJson.js"; System.out.println(s); //将前台传来的json变成java中class对象 String ss = ToString.readData(s); System.out.println(ss); Semantics ds=new Semantics(); Gson gson = new Gson(); ds = gson.fromJson(ss, Semantics.class); System.out.println(ds); System.out.println("DeleteSemantics.getDataset().getSourceList().getAliases()="+ds.getOptionList().get(0).getConditions().getConditionsList().get(0).getConditionsList().get(0).getConditionsList().get(1).getConditionList().get(1).getFuncList().get(1).getInput()); System.out.println("success"); //将class对象转化为jsonString String jsonStr=gson.toJson(ds); System.out.println(jsonStr); //将json字符串变成json对象 JSONObject myJsonObject = null; try { myJsonObject = new JSONObject(jsonStr); System.out.println(myJsonObject.get("dataset")); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } //java中的json对象可以使用put(key,value)进行添加,也可以使用get(key)提取对应的value----有点像map try { System.out.println(myJsonObject.get("dataset"));//因为dataset下还是json,则输出的还是json System.out.println(myJsonObject.get("id"));//因为dataset下还是id,则输出的是string String w = "{'name':'sff'}"; JSONObject jsonObj = new JSONObject(w); jsonObj.put("age", "23"); myJsonObject.put("people", jsonObj); System.out.println("myJsonObject:"+myJsonObject.get("people")); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
相关文章推荐
- 服务器部署java项目
- Spring MVC
- Java编程思想第四版第十二章学习——通过异常处理错误(2)
- SpringMVC访问静态资源的三种方式
- mac切换jdk版本
- java四种线程池
- JAVA8开发指南第一章
- java框架之访问首页
- XStream 常用注释 (去子节点集合)
- Java接口和继承
- Java 利用Xstream 解析 XML
- [Java] Date 与 String 和 Timestamp 相互转化
- java.util.regex
- Java字节码与Dalvik字节码
- Java 读取文件内容 方法集锦
- JAVA文档注释小结
- 数字转字符串前面自动补0 / 字符串(数字串)除去前面的0
- Spring mvc 提交时间 String Date 的转换问题
- 构建微服务:Spring boot 入门篇
- spring 多数据源一致性事务方案