json-lib Quick Start

2010-12-23  来源:本站原创  分类:Mobile  人气:150 

1) JSON Introduction
2) JSON / LIST conversion
3) JSON / MAP conversion
4) JSON / Dynamic Bean conversion
5) JSON / static Bean conversion
6) JSON / XML Output

1.JSON Introduction
JSON (JavaScript Object Notation) is a lightweight data interchange format, based on JavaScript, but not limited to this.
Details can refer www.json.org
For example, a XML
<? Xml version = "1.0" encoding = "utf-8"?>
<shop>
<name> Hotel </ name>
<city> Beijing </ city>
</ Shop>
Use JSON expressed as follows:
{
"Name": "Hotel"
"City": "Beijing"
}
XML parsing may consider the parent child relationship, and the difficulty of JSON parsing is very low, a lot of control, particularly related to data exchange ajax Many use json.

2) JSON / LIST conversion used in this tutorial parsing json-lib, the official website for the http://json-lib.sourceforge.net/ , this tutorial reference configuration of the official tutorial environment requires the following jar
commons-beanutils, and ezmorph control reflex
commons-collections is a child of apachecommons extends the java collections
commons-lang extends java.lang package
commons-logging Log class
xom is xml parsing class, you can refer www.xom.nu
The jar test junit unit
json-lib core jar
Project folder in the possession of the goods parameters (Shop) and goods argument list (ShopList) two entities
Shop name and the property contains two fields, ShopList contains a list of corresponding json Shop is
String s = "[{name: 'weight', property: 'p1'}, {name: 'size', property: 'p2'}, {name: 'card type', property: 'p3'}, {name : 'hard disk capacity', property: 'p4'}, {name: 'processor', property: 'p5'}, {name: 'memory', property: 'p6'}, {name: 'model', property : 'p7'}, {name: 'Item', property: 'p8'}, {name: 'brand', property: 'p9'}]";
The data structure such as a user-defined personal information stored in the database up to the role of individual parameters,
For example shopex many tables in the database is to use the json data type. Because users add their own parameters are not fixed length such as the above example to have nine user-defined parameters when a user need to fill these parameters, the need into a list, then the view in the struts2 code to display the completed reference ArrayUtil file attachment to the core code is only one line JSONArray jsonArray = JSONArray.fromObject (s);
After going to get this jsonArray into ArrayList, need to loop through, as
for (int i = 0; i <jsonArray.size (); i + +) {
Object o = jsonArray.get (i);
JSONObject jsonObject = JSONObject.fromObject (o);
Shop Person = (Shop) JSONObject.toBean (jsonObject, Shop.class);
list.add (Person);
}
Then get a list is an ordinary ArrayList

3) JSON / MAP conversion completed when we initialize a map, you can put directly into json
Map <String, String> map = new HashMap <String, String> ();
map.put ("name", "weight");
map.put ("property", "p1");
JSONObject jsonObject = JSONObject.fromObject (map);
Core code
JSONObject jsonObject = JSONObject.fromObject (map);
JsonLib will automatically complete the examples in Annex MapUtil.java map

4) JSON / Dynamic Bean convert the so-called dynamic java bean that is created when running under the circumstances, not the programmer's Bean has a good
JsonLib Json format data is automatically created based on the field, and then create an Object containing the field
This example uses JUNIT do unit testing validation, see DynamicBean.java
String s = "{name: 'weight', property: 'p1'}";
JSONObject jsonObject = JSONObject.fromObject (s);
Object bean = JSONObject.toBean (jsonObject);
assertEquals (jsonObject.get ("name"), PropertyUtils.getProperty (bean, "name"));
assertEquals (jsonObject.get ("property"), PropertyUtils.getProperty (bean, "property"));

5) JSON / static Bean conversion (StaticBean.java)
JSONLIB when the conversion will automatically find the relationship, such as sub-classes and parent classes such as JSON data source
String s = "{'shopList': [{name: 'weight', property: 'p1'}, {name: 'size', property: 'p2'}, {name: 'card type', property: 'p3 '}, {name:' hard disk capacity ', property:' p4 '}, {name:' processor ', property:' p5 '}, {name:' memory ', property:' p6 '}, {name: 'Model', property: 'p7'}, {name: 'Item', property: 'p8'}, {name: 'brand', property: 'p9'}]}";
Deposited Map
map.put ("shopList", Shop.class);
ShopList shopList = (ShopList) JSONObject.toBean (JSONObject.fromObject (s), ShopList.class, map);
JSONObject.toBean () method of the three parameters, respectively, corresponding to the JSON data source object, the object of transformation and the data source map ShopList
Then this can be achieved ShopList
This method and the dynamic conversion of difference is just the dynamic conversion into Object
The static conversion is converted into an entity class is already defined, it will automatically map (this is similar to the Ibatis)

6) JSON / XML output method if they use the String into the XML output to write a lot of code, and then be used JSONLIB, the core code is only one step
String xmlObject = xmlSerializer.write (object);
Such as
String s = "{name: 'weight', property: 'p1'}";
XMLSerializer xmlSerializer = new XMLSerializer ();
JSONObject object = JSONObject.fromObject (s);
String xmlObject = xmlSerializer.write (object);
System.out.println (xmlObject);

Output results
<? Xml version = "1.0" encoding = "UTF-8"?>
<o>
<name type="string"> weight </ name>
<property type="string"> p1 </ property>
</ O>

相关文章
  • JSON lib 2009-09-27

    JSON lib 网站 : http://json-lib.sourceforge.net JSON-lib 是一个用来映射 JSON 到 Java Bean 的一个 Java 类库. 示例: http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html 授权协议: Apache Licene 2.0 开发语言: Java 操作系统: 跨平台

  • struts2.1.8 the JSON lib environmental settings 2010-06-27

    starting from the 2.1.8 version of struts, JSON's lib from 0.34 directly to upgrade to GWT struts2-json-plugin2.1.8 About lib settings, with little difference between the previous version, but the class will be the default address and intercetper add

  • json lib and the java switch between handling json 2010-03-29

    Usually json-lib to handle the conversion between the java and json. Download address: http://json-lib.sourceforge.net/ Data conversion between the two as follows: JSON <=> java string <=> java.lang.String, java.lang.Character, char number <

  • json lib Study Notes 2010-02-10

    json-lib demo JSON-lib to this Java class package for the bean, map, and XML into JSON, and be able to JSON back into the bean and DynaBean. The http://json-lib.sourceforge.net Download json-lib-1.1-jdk15.jar, also need to run the json package are: j

  • json lib conversion time 2010-11-11

    Java Date object types of objects exist can not be resolved, but I have to use the Date type, but fortunately, json-lib provides a scalable interface JsonValueProcessor interface to let us customize the interface processing Json return value to see t

  • Use JSON to send data to the server 2009-11-11

    Has done so much, you are more comfortable to use JavaScript, and perhaps in the model to consider more information on the browser. However, after reading the previous example (the use of XML to the server to send complex data structures), you may ch

  • Mutual Convert Java objects and JSON string 2010-05-12

    Mutual Convert Java objects and JSON string Uncategorized , by admin. Now the products are functional and the front jsp page is to compare large data exchange, so consider using JSON for data exchange. Original use of the JSON lib , the truth is real

  • JSON格式校验 2011-06-30

    第一种,没有双引号的key,它也是可以准确无误的通过各种json lib的转换,以及js的解析(当然js的对象的key本来就可以不用双引号包围起来. [ [{name: "brandId1", type: "String1"},{name: "brandName1", type: "String1"}], [{name: "brandId2", type: "String2"},{nam

  • Java解析Json数据 2013-03-04

    方式一:使用json.lib.jar解析 需要依赖包: ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar jsoup-1.6.1.jar commons-beanutils-1.8.0.jar commons-collections.jar commons-lang-2.4.jar commons-logging-1.1.1.jar 加入缺少某一个包,会报相应的异常: commons-beanutils-1.8.0.jar java.lang.NoClassDef

  • Rails use to achieve cascade EXT Tree Tree 2009-11-01

    Js first download the ext package, you can download the http://extjs.com/, there are two versions 1.0 and 2.0, if you download the package named after ext1.0 or remove the version number 2.0, unified into ext to facilitate the path to write the name

  • google Gson study notes and comparison with the json-lib 2010-08-13

    Because gson online help documents open slow, so to help extract this document to facilitate the view: 1. Basic types of transformation public static void main(String[] args) { Gson gson = new Gson(); System.out.println(gson.toJson(1)); // ==> prints

  • org / apache / commons / collections / map / ListOrderedMap 2010-09-06

    When using the json class file not found exception: org / apache / commons / collections / map / ListOrderedMap First of all, used to analyze the json lib package roughly about a few: Common to the already included in the struts of a commons-logging-

  • Report on the struts2 There is no Action mapped for namespace / and action name xxx_xxx 2010-12-15

    The hands of a SSH on-line project soon, and I do not know how suddenly drops down from the server to run up and down the project after the call to an error page, but are invariably There is no Action mapped for namespace / and action name login_logi

  • maven打包 2013-02-25

    今天用maven管理项目,折腾半天,打包成功,具体做法如下:先编译(如果有额外的配置文件,请将配置文件放在编译后的classes文件下对应的文件下面)---打包----ok! 我想只需要修改你的jar仓库和相应依赖的jar,直接按照上面所说的去做,maven打包应该是没有多大的问题的: maven收索jar,http://mvnrepository.com/ one-jar: plugin 不需要将依赖的jar解开,直接打包到目标jar <plugin> <groupId>com.

  • VS 2010 配置jsoncpp 库 2013-09-11

    详细地jsoncpp编译方法 和 vs2010中导入第三方库的方法 一 编译链接 1 在相应官网下载jsoncpp 2 解压得到jsoncpp-src-0.5.0文件 3 打开jsoncpp-src-0.5.0 -> makefiles -> vs71 -> jsoncpp.sln 4 转换项目为VS2010格式 5 选择debug模式 6 在"解决方案资源管理器"中右击 lib_json 选择->仅用于项目 -> 仅生成lib_json 7 再次右击 l

  • spring mvc+spring3.05+mybatis3.1.1环境搭建____maven管理 2015-01-10

    =====================pom.xml=================================================== <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.or

  • 将json当数据库一样操作的javascript lib 2013-10-20

    使用javascript操作JSON的类库TAFFY DB,具体介绍了:查询数据.添加数据.删除数据.修改数据. 前段时间工作中用到Json数据,希望将一些简单的增删改查放到客户端来做,这样也能减少服务器端的压力.分别查找了几个可以对Json进行操作的javascript 类库,最终选定了TAFFY DB.原因如下: 1.使用的是BSD开源协议,不用担心商业使用限制. 2.一直在有人进行更新,并且不和其他框架(如Jquery,YUI,Dojo等)冲突 3.功能齐全--增删改查.排序等等都有了.

  • jQuery / Ajax / PHP / Json example of an integrated collection 2009-03-07

    jQuery excellent Javascript is a framework for a good js packaging, provides a lot of convenient features. jQuery for ajax packaging can be called outstanding. jQuery can json file transfer protocol to transmit data (similar to xml, but also signific

  • About JSON 2009-06-28

    Abstract XML - this for that client and server data exchange between the payload format has become almost synonymous with Web services. However, because Ajax and REST technology applications that affected the structure, forcing people to seek alterna

  • Struts 2 in the use of JSON Ajax support 2009-08-20

    JSON plugin provides a json called the ResultType, once designated for a certain type of Action for the json of Result, without the Result of resources mapped to any view. JSON plugin because Action will be responsible for the status information in t