JSON的一点自己的理解
2012-02-13 15:07
323 查看
今天由于帮朋友用ajax实现autocomplete的功能,用到json 然后就看了下 有点理解 写下来以防忘记
里面很多是查资料查出来的,然后附带自己的整理和理解
java中操作json:
1:创建JSONObject
上面是最简单的创建jsonObject的方法
2:JsonConfig 的介绍
JsonConfig :
Utility class that helps configuring the serialization process.
创建JSONObject时可以使用JsonConfig 进行配置
查看JsonConfig 时感觉其中有两个方法比较实用(暂时只用了其中这两个方法)都是进行过滤掉不需要的属性
1)setExcludes方法
例子:
2)由于上面的方法只是配置了需要排除的属性 但有时我们的需求是取出beans的某些属性进行转换成json如果用上面的方法就会有点复杂(利用反射机制应该可以做)
接下的方法就是setJavaPropertyFilter
这里用到了PropertyFilter属性过滤
它是一个接口 只有一个方法:
我们可以实现PropertyFilter 重写apply方法 进行设置需要过滤的方法
3:有了2的只是我们就可以轻松用java操作json了
然后就是JsonObject转换成javabean了
4:将获得的json传递至页面
5:json可以不用值对形式出现
这样得到的json对象为["test1","test2"]
前台通过ajax设置type为json可以读取数据
直接通过数组形式读取内容
里面很多是查资料查出来的,然后附带自己的整理和理解
java中操作json:
1:创建JSONObject
private static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("username","Ouvidia"); jsonObject.put("sex", "M"); return jsonObject; }
上面是最简单的创建jsonObject的方法
2:JsonConfig 的介绍
JsonConfig :
Utility class that helps configuring the serialization process.
创建JSONObject时可以使用JsonConfig 进行配置
查看JsonConfig 时感觉其中有两个方法比较实用(暂时只用了其中这两个方法)都是进行过滤掉不需要的属性
1)setExcludes方法
public void setExcludes(String[] excludes) Sets the excludes to use. Will set default value ([]) if null. [Java -> JSON]
例子:
public void testExcludeProperites() { String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[] { "double", "boolean" }); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str, jsonConfig); System.out.println(JSONObject.toString()); }
2)由于上面的方法只是配置了需要排除的属性 但有时我们的需求是取出beans的某些属性进行转换成json如果用上面的方法就会有点复杂(利用反射机制应该可以做)
接下的方法就是setJavaPropertyFilter
setJavaPropertyFilter public void setJavaPropertyFilter(PropertyFilter javaPropertyFilter)Sets a property filter used when serializing to Java. [JSON -> Java] Parameters: javaPropertyFilter - the property filter
这里用到了PropertyFilter属性过滤
它是一个接口 只有一个方法:
boolean apply(Object source, String name, Object value)Parameters: source - the owner of the property name - the name of the property value - the value of the property Returns: true if the property will be filtered out, false otherwise
我们可以实现PropertyFilter 重写apply方法 进行设置需要过滤的方法
protected PropertyFilter filter(final String [] excludes) { return new PropertyFilter() { public boolean apply(Object arg0, String arg1, Object arg2) { boolean bool = false; for (int i = 0; i < excludes.length; i++) { if (arg1.equals(excludes[i])) { bool = true; } else { bool = false; } } return bool; } }; }
3:有了2的只是我们就可以轻松用java操作json了
JSONObject jsonObject=JSONObject.fromObject(obj); //jsonArray JSONArray jsonArray=JSONArray.fromObject(obj); for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); }
然后就是JsonObject转换成javabean了
JSONObject.toBean(jsonObject, clazz)
4:将获得的json传递至页面
response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonObject.toString());
5:json可以不用值对形式出现
JSONArray jso=new JSONArray(); jso.add(0, "test1"); jso.add(1,"test2"); System.out.println(jso.toString());
这样得到的json对象为["test1","test2"]
前台通过ajax设置type为json可以读取数据
直接通过数组形式读取内容
相关文章推荐
- 自己对权限表的一点理解
- 对Silverlight中ObservableCollection自己的一点理解 .
- 自己关于Intent的一点大致的理解
- 关于json的一点理解
- 浅谈json,自己初认识json,写下自己对其概念的理解。
- 对JSON的一点认识和理解以及JQuery处理JSON
- php中 ->,self,::,static 自己的一点理解
- 自己对java里面的socket的一点理解
- 自己对java数据结构跟算法的一点理解把
- 自己对shell,终端的一点理解
- HADOOP之NAMENODE元数据管理(一点理解记下来只是为了帮助自己记忆)
- 自己对android开机速度优化的一点理解
- 关于call ,this,继承,记录一点自己的初步理解
- 黑马程序员--Io流,(含自己的一点理解,与图解)
- java中的回调机制(自己的一点理解)
- 关于spring mvc的一点自己的理解
- 编程之美1.2中国象棋将帅问题——转载+自己的一点理解
- CountDownLatch 自己的一点理解
- 对于session与cookie机制,自己的一点理解
- 自己在学习Mybits的时候理解的一点sql注入的知识,以及处理的方法!