当前位置:文档之家› JSONObject简介

JSONObject简介

JSONObject简介本节摘要:之前对JSON做了一次简单的介绍,并把JSON和XML做了一个简单的比较;那么,我就在想,如果是一个json格式的字符串传到后台,需要怎么对其处理?如果前台页面需要后台返回的是一个json的字符串,那么后台是怎么把json格式的字符串给拼装起来的呢?JSON和XML是不是有某种联系,他们之间是不是可以互相转换?……带着这些问题,搜索了相关的资料,并自己实际动手操作,总算对这些问题有了个比较清晰的认识。

这些问题主要是通过JSONObject这个插件的jar包实现。

preparation1.JSONObject介绍JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包。

2.下载jar包/java-pan/lib.rar提供了除JSONObject的jar之外依赖的其他6个jar包,一共7个jar文件说明:因为工作中项目用到的版本是1.1的对应jdk1.3的版本,故本篇博客是基于1.1版本介绍的。

对应此版本的javadoc下载路径如下:/projects/json-lib/files/json-lib/json-lib-1.1/目前最新的版本为2.4,其他版本下载地址为/projects/json-lib/files/json-lib/3.项目环境:system:WIN7 myeclipse:6.5 tomcat:5.0 JDK:开发环境和编译用的都是1.5项目结构如下:说明:本次用到的的文件只有工程目录json包下的JSONObject_1_3类和note.txt4.class&method 基于1.1的API做以下几点约定:1.介绍基于JSONObject 1.1的API2.只介绍常用的类和方法3.不再介绍此版本中已经不再推荐使用4.介绍的类和方法主要围绕本篇博客中用到的JSONObject:AJSONObject is an unordered collection of name/value pairs.是一个final类,继承了Object,实现了JSON接口构造方法如下:JSONObject();创建一个空的JSONObject对象JSONObject(booleanisNull);创建一个是否为空的JSONObject对象普通方法如下:fromBean(Object bean);静态方法,通过一个pojo对象创建一个JSONObject对象fromJSONObject(JSONObject object);静态方法,通过另外一个JSONObject对象构造一个JSONObject对象fromJSONString(JSONString string);静态方法,通过一个JSONString创建一个JSONObject对象toString();把JSONObject对象转换为json格式的字符串iterator();返回一个Iterator对象来遍历元素接下来就是一些put/get方法,需要普通的get方法和pot方法做一下强调说明,API中是这样描述的:A get method returns a value if one can be found, and throws an exception if one cannot be found. An opt method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.JSONArray:AJSONArray is an ordered sequence of values.是一个final类,继承了Object,实现了JSON接口构造方法如下:JSONArray();构造一个空的JSONArray对象普通方法如下:fromArray(Object[] array);静态方法,通过一个java数组创建一个JSONArray 对象fromCollection(Collection collection);静态方法,通过collection集合对象创建一个JSONArray对象fromString(String string);静态方法,通过一个json格式的字符串构造一个JSONArray对象toString();把JSONArray对象转换为json格式的字符串iterator();返回一个Iterator对象来遍历元素接下来同样是put/get方法……XMLSerializer:Utility class for transforming JSON to XML an back.一个继承自Object的类构造方法如下:XMLSerializer();创建一个XMLSerializer对象普通方法如下:setRootName(String rootName);设置转换的xml的根元素名称setTypeHintsEnabled(boolean typeHintsEnabled);设置每个元素是否显示type属性write(JSON json);把json对象转换为xml,默认的字符编码是UTF-8,需要设置编码可以用write(JSON json, String encoding)5.对XML和JSON字符串各列一个简单的例子JSON:{"password":"123456","username":"张三"}xml<?xml version="1.0" encoding="UTF-8"?><user_info><password>123456</password><username>张三</username></user_info>start新建web工程,工程名称JS,导入以下7个jar包,文件在前面的准备工作中下载路径。

说明:可以不用新建web工程,普通的java工程也可以完成本篇的的操作。

至于为什么要导入处json包以外的其他6个包,我会把note.txt贴在最后,各位一看便知。

故以上7个jar包必须导入。

question1:后台接受到前台的json格式的字符串怎么处理?public static void jsonToJAVA(){System.out.println("json字符串转java代码");String jsonStr="{\"password\":\"123456\",\"username\":\"张三\"}"; JSONObjectjsonObj=JSONObject.fromString(jsonStr);String username=jsonObj.getString("username");String password=jsonObj.optString("password");System.out.println("json--->java\n username="+username+"\tpassword="+password);}question2:后台是怎么拼装json格式的字符串?public static void javaToJSON() {System.out.println("java代码封装为json字符串");JSONObjectjsonObj = new JSONObject();jsonObj.put("password", "123456");System.out.println("java--->json \n" + jsonObj.toString());}question3:json格式的字符串怎么转换为xml格式的字符串?public static void jsonToXML() {System.out.println("json字符串转xml字符串");String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}"; JSONObjectjson = JSONObject.fromString(jsonStr); XMLSerializerxmlSerializer = new XMLSerializer();xmlSerializer.setRootName("user_info");xmlSerializer.setTypeHintsEnabled(false);String xml = xmlSerializer.write(json);System.out.println("json--->xml \n" + xml);}question4:xml格式的字符串怎么转换为json格式的字符串?public static void xmlToJSON(){System.out.println("xml字符串转json字符串");String xml = "<?xml version=\"1.0\"encoding=\"UTF-8\"?><user_info><password>123456</password><userna me>张三</username></user_info>";JSON json=XMLSerializer.read(xml);System.out.println("xml--->json\n"+json.toString());}question5:javabean怎么转换为json字符串?public static void javaBeanToJSON() {System.out.println("javabean转json字符串");UserInfouserInfo = new UserInfo();userInfo.setPassword("123456");JSONObjectjson = JSONObject.fromBean(userInfo);System.out.println("javabean--->json \n" + json.toString());}question6:javabean怎么转换为xml字符串?public static void javaBeanToXML() {System.out.println("javabean转xml字符串");UserInfouserInfo = new UserInfo();userInfo.setUsername("张三");userInfo.setPassword("123456");JSONObjectjson = JSONObject.fromBean(userInfo); XMLSerializerxmlSerializer = new XMLSerializer();String xml = xmlSerializer.write(json, "UTF-8");System.out.println("javabean--->xml \n" + xml);}result代码和运行结果都已经贴在每个问题的后面,运行时直接用main方法分别对每个方法运行即可看到测试效果。

相关主题