- 浏览: 218544 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (397)
- j2se (28)
- nio (3)
- 易错点 (3)
- 面试ssh (9)
- ssh整合 (11)
- jbpm+spring (2)
- js (15)
- 高级技术 (59)
- swing (3)
- 数据库 (16)
- hibernate (18)
- spring (19)
- 开发网站知识点 (9)
- jbpm (4)
- json (5)
- 设计模式 (22)
- 自定义标签 (1)
- j2ee (9)
- lucene (3)
- cahce (11)
- maven (5)
- html5 (1)
- 多数据源 (10)
- 页面聊天 (9)
- 富客户端 (1)
- android (13)
- aop+拦截器+jms (13)
- 框架整合 (1)
- 非阻塞io (24)
- 暂时不看 (13)
- webservice (3)
- oracle (3)
- 算法 (4)
- 协程 (2)
- netty (1)
- 爬虫 (0)
- 高级基础 (1)
- JVM调优总结 (12)
- 知识点技巧 (1)
- REST (0)
- 基础 io (2)
- dubbo (8)
- 线程 (1)
- spring源码 (2)
- git (1)
- office (2)
最新评论
-
sjzcmlt:
,写的挺好的啊
一个完整的负载均衡的例子 . -
他大姨妈:
网上大部分例子都是直接通过IdleStateHandler来实 ...
Netty的超时机制 心跳机制
package com.mai.json;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;
public class JsonLibTest {
public static void main(String[] args) {
// testArrayToJSON();
// testListToJSON();
// testJsonStrToJSON();
// testMapToJSON();
testBeadToJSON2();
}
public static void testJSONObject() {
JSONObject JSON = new JSONObject();
JSON.put("username","huangwuyi");
JSON.put("sex", "男");
JSON.element("address", "福建省");
System.out.println("jsonObject==>"+JSON);
//{"username":"huangwuyi","sex":"男","address":"福建省"}
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "array1");
jsonArray.add(1,"array2");
JSON.element("jsonArray", jsonArray);
JSONArray array = JSON.getJSONArray("jsonArray");
System.out.println("JSONArray==>"+array);
//["array1","array2"]
System.out.println("结果="+JSON);
//{"username":"huangwuyi","sex":"男","address":"福建省","jsonArray":["array1","array2"]}
String temp=JSON.toString();
JSONObject object = JSONObject.fromObject(temp);
System.out.println("username="+object.get("username"));
}
private static void testBeadToJSON2() {
TestObjectToJson obj = new TestObjectToJson();
Gson gson = new Gson();
String json = gson.toJson(obj);
System.out.println(json);
//{"id":100,"name":"hello"}
String jsonString="{'id':100,'name':'hello'}";
TestObjectToJson toObj = gson.fromJson(json, TestObjectToJson.class);
}
static class TestObjectToJson {
private int id = 100;
private String name = "hello";
}
private static void testBeadToJSON() {
JsonArray array = new JsonArray();
JsonObject top = new JsonObject();
top.addProperty("id", "ii");
top.addProperty("pId", "pp");
top.addProperty("name", "nn");
top.addProperty("lvl", "1");
array.add(top);
JsonObject second = new JsonObject();
second.addProperty("id", "i2");
second.addProperty("pId", "p2");
second.addProperty("name", "n2");
second.addProperty("lvl", "2");
array.add(second);
System.out.println(top.toString());
// {"id":"ii","pId":"pp","name":"nn","lvl":"1"}
System.out.println(array.toString());
// [{"id":"ii","pId":"pp","name":"nn","lvl":"1"},{"id":"i2","pId":"p2","name":"n2","lvl":"2"}]
}
public static void testArrayToJSON() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
// prints [true,false,true]
}
public static void testListToJSON() {
List list = new ArrayList();
list.add("first");
list.add("second");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
// prints ["first","second"]
}
public static void testJsonStrToJSON() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray);
// prints ["json","is","easy"]
}
public static void testMapToJSON() {
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println(jsonObject);
// {"arr":["a","b"],"int":1,"name":"json","func":function(i){ return this.arr[i]; },"bool":true}
}
//复合类型bean转成成json
@Test
public void testBeadToJSON(){
MyBean bean = new MyBean();
bean.setId("001");
bean.setName("银行卡");
bean.setDate(new Date());
List cardNum = new ArrayList();
cardNum.add("农行");
cardNum.add("工行");
cardNum.add("建行");
cardNum.add(new Person("test"));
bean.setCardNum(cardNum);
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
//普通类型的json转换成对象
@Test
public void testJSONToObject() throws Exception{
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromObject( json );
System.out.println(jsonObject);
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
System.out.println(PropertyUtils.getProperty(bean, "name"));
System.out.println(PropertyUtils.getProperty(bean, "bool"));
System.out.println(PropertyUtils.getProperty(bean, "int"));
System.out.println(PropertyUtils.getProperty(bean, "double"));
System.out.println(PropertyUtils.getProperty(bean, "func"));
System.out.println(PropertyUtils.getProperty(bean, "array"));
List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("array"));
for(Object object : arrayList){
System.out.println(object);
}
}
//将json解析成复合类型对象, 包含List
@Test
public void testJSONToBeanHavaList(){
String json = "{list:[{name:'test1'},{name:'test2'}],map:{test1:{name:'test1'},test2:{name:'test2'}}}";
// String json = "{list:[{name:'test1'},{name:'test2'}]}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap);
System.out.println(diyBean);
List list = diyBean.getList();
for(Object o : list){
if(o instanceof Person){
Person p = (Person)o;
System.out.println(p.getName());
}
}
}
//将json解析成复合类型对象, 包含Map
@Test
public void testJSONToBeanHavaMap(){
//把Map看成一个对象
String json = "{list:[{name:'test1'},{name:'test2'}],map:{testOne:{name:'test1'},testTwo:{name:'test2'}}}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
classMap.put("map", Map.class);
//使用暗示,直接将json解析为指定自定义对象,其中List完全解析,Map没有完全解析
MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap);
System.out.println(diyBean);
System.out.println("do the list release");
List<Person> list = diyBean.getList();
for(Person o : list){
Person p = (Person)o;
System.out.println(p.getName());
}
System.out.println("do the map release");
//先往注册器中注册变换器,需要用到ezmorph包中的类
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
Morpher dynaMorpher = new BeanMorpher( Person.class, morpherRegistry);
morpherRegistry.registerMorpher( dynaMorpher );
Map map = diyBean.getMap();
/*这里的map没进行类型暗示,故按默认的,里面存的为net.sf.ezmorph.bean.MorphDynaBean类型的对象*/
System.out.println(map);
/*输出:
{testOne=net.sf.ezmorph.bean.MorphDynaBean@f73c1[
{name=test1}
], testTwo=net.sf.ezmorph.bean.MorphDynaBean@186c6b2[
{name=test2}
]}
*/
List<Person> output = new ArrayList();
for( Iterator i = map.values().iterator(); i.hasNext(); ){
//使用注册器对指定DynaBean进行对象变换
output.add( (Person)morpherRegistry.morph( Person.class, i.next() ) );
}
for(Person p : output){
System.out.println(p.getName());
/*输出:
test1
test2
*/
}
}
}
5.下面提供上面例子所需的资源,包括jar包和代码
/Files/mailingfeng/json-lib/json-lib用例所需jar包和java类.rar
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;
public class JsonLibTest {
public static void main(String[] args) {
// testArrayToJSON();
// testListToJSON();
// testJsonStrToJSON();
// testMapToJSON();
testBeadToJSON2();
}
public static void testJSONObject() {
JSONObject JSON = new JSONObject();
JSON.put("username","huangwuyi");
JSON.put("sex", "男");
JSON.element("address", "福建省");
System.out.println("jsonObject==>"+JSON);
//{"username":"huangwuyi","sex":"男","address":"福建省"}
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "array1");
jsonArray.add(1,"array2");
JSON.element("jsonArray", jsonArray);
JSONArray array = JSON.getJSONArray("jsonArray");
System.out.println("JSONArray==>"+array);
//["array1","array2"]
System.out.println("结果="+JSON);
//{"username":"huangwuyi","sex":"男","address":"福建省","jsonArray":["array1","array2"]}
String temp=JSON.toString();
JSONObject object = JSONObject.fromObject(temp);
System.out.println("username="+object.get("username"));
}
private static void testBeadToJSON2() {
TestObjectToJson obj = new TestObjectToJson();
Gson gson = new Gson();
String json = gson.toJson(obj);
System.out.println(json);
//{"id":100,"name":"hello"}
String jsonString="{'id':100,'name':'hello'}";
TestObjectToJson toObj = gson.fromJson(json, TestObjectToJson.class);
}
static class TestObjectToJson {
private int id = 100;
private String name = "hello";
}
private static void testBeadToJSON() {
JsonArray array = new JsonArray();
JsonObject top = new JsonObject();
top.addProperty("id", "ii");
top.addProperty("pId", "pp");
top.addProperty("name", "nn");
top.addProperty("lvl", "1");
array.add(top);
JsonObject second = new JsonObject();
second.addProperty("id", "i2");
second.addProperty("pId", "p2");
second.addProperty("name", "n2");
second.addProperty("lvl", "2");
array.add(second);
System.out.println(top.toString());
// {"id":"ii","pId":"pp","name":"nn","lvl":"1"}
System.out.println(array.toString());
// [{"id":"ii","pId":"pp","name":"nn","lvl":"1"},{"id":"i2","pId":"p2","name":"n2","lvl":"2"}]
}
public static void testArrayToJSON() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
// prints [true,false,true]
}
public static void testListToJSON() {
List list = new ArrayList();
list.add("first");
list.add("second");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
// prints ["first","second"]
}
public static void testJsonStrToJSON() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray);
// prints ["json","is","easy"]
}
public static void testMapToJSON() {
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println(jsonObject);
// {"arr":["a","b"],"int":1,"name":"json","func":function(i){ return this.arr[i]; },"bool":true}
}
//复合类型bean转成成json
@Test
public void testBeadToJSON(){
MyBean bean = new MyBean();
bean.setId("001");
bean.setName("银行卡");
bean.setDate(new Date());
List cardNum = new ArrayList();
cardNum.add("农行");
cardNum.add("工行");
cardNum.add("建行");
cardNum.add(new Person("test"));
bean.setCardNum(cardNum);
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
//普通类型的json转换成对象
@Test
public void testJSONToObject() throws Exception{
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromObject( json );
System.out.println(jsonObject);
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
System.out.println(PropertyUtils.getProperty(bean, "name"));
System.out.println(PropertyUtils.getProperty(bean, "bool"));
System.out.println(PropertyUtils.getProperty(bean, "int"));
System.out.println(PropertyUtils.getProperty(bean, "double"));
System.out.println(PropertyUtils.getProperty(bean, "func"));
System.out.println(PropertyUtils.getProperty(bean, "array"));
List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("array"));
for(Object object : arrayList){
System.out.println(object);
}
}
//将json解析成复合类型对象, 包含List
@Test
public void testJSONToBeanHavaList(){
String json = "{list:[{name:'test1'},{name:'test2'}],map:{test1:{name:'test1'},test2:{name:'test2'}}}";
// String json = "{list:[{name:'test1'},{name:'test2'}]}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap);
System.out.println(diyBean);
List list = diyBean.getList();
for(Object o : list){
if(o instanceof Person){
Person p = (Person)o;
System.out.println(p.getName());
}
}
}
//将json解析成复合类型对象, 包含Map
@Test
public void testJSONToBeanHavaMap(){
//把Map看成一个对象
String json = "{list:[{name:'test1'},{name:'test2'}],map:{testOne:{name:'test1'},testTwo:{name:'test2'}}}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
classMap.put("map", Map.class);
//使用暗示,直接将json解析为指定自定义对象,其中List完全解析,Map没有完全解析
MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap);
System.out.println(diyBean);
System.out.println("do the list release");
List<Person> list = diyBean.getList();
for(Person o : list){
Person p = (Person)o;
System.out.println(p.getName());
}
System.out.println("do the map release");
//先往注册器中注册变换器,需要用到ezmorph包中的类
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
Morpher dynaMorpher = new BeanMorpher( Person.class, morpherRegistry);
morpherRegistry.registerMorpher( dynaMorpher );
Map map = diyBean.getMap();
/*这里的map没进行类型暗示,故按默认的,里面存的为net.sf.ezmorph.bean.MorphDynaBean类型的对象*/
System.out.println(map);
/*输出:
{testOne=net.sf.ezmorph.bean.MorphDynaBean@f73c1[
{name=test1}
], testTwo=net.sf.ezmorph.bean.MorphDynaBean@186c6b2[
{name=test2}
]}
*/
List<Person> output = new ArrayList();
for( Iterator i = map.values().iterator(); i.hasNext(); ){
//使用注册器对指定DynaBean进行对象变换
output.add( (Person)morpherRegistry.morph( Person.class, i.next() ) );
}
for(Person p : output){
System.out.println(p.getName());
/*输出:
test1
test2
*/
}
}
}
5.下面提供上面例子所需的资源,包括jar包和代码
/Files/mailingfeng/json-lib/json-lib用例所需jar包和java类.rar
相关推荐
json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于...
最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json...
采用pb11.5 + pbni + vs2015 + rapidjson的开源库,生成解析json,支持datawindow快速导入导出字段名有大写字母的json,支持dw导入出json时对指定字段进行des加密,并在导入到dw时时进行des解密,修改了pbvm115.dll...
json11::Json jsonObject = json11::Json::parse(jsonString); if (jsonObject.is_object()) { // 处理解析成功的对象 } else { // 处理解析错误 } ``` 一旦你有了JSON对象,可以访问其成员或进行修改。例如,...
json3.js 【JS / JavaScript 中解析JSON的js包,JSON官方的JSON解析包】。JavaScript中解析JSON的js包,页面中引入json3.js,即可使用。 使用方法:JSON.parse(str), JSON.stringify(obj) 更多详情请参考博文: ...
Java JSON API是Java平台上的库,提供了处理JSON的能力,包括解析JSON字符串、生成JSON对象以及进行JSON与Java对象之间的转换。 在Java中,有多种实现JSON API的库,如Jackson、Gson、org.json和json-lib等。本篇...
而JSON Schema则是一个JSON格式的规范,用于定义JSON数据的结构和限制,类似于XML Schema,它为JSON数据提供了验证规则,确保数据的准确性和一致性。 在JavaScript开发中,有时我们需要将JSON对象转换为JSON Schema...
标题中的"ZUI2_JSON2_/UI2/CL_JSON_ui2/cl_json_abap_Ui2_cl_json_zui2_json_源"可能指的是一个与ZUI2相关的项目,它利用了ABAP类/UI2/CL_JSON来解析和生成JSON数据。描述中提到,我们需要用到这个类来将JSON格式的...
在这个例子中,我们首先使用`json_tokener_parse`解析JSON字符串,然后通过`json_object_get_string`和`json_object_get_int`获取JSON对象中的数据,最后使用`json_object_put`释放内存。这就是`json-c`库基本的使用...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于Web服务和应用程序之间的数据传输。它易于人阅读和编写,同时也易于机器解析和生成。`net.sf.json`是开源项目Apache软件基金会下的一个...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于Web应用程序之间传输数据。它以文本形式存储和传递数据,易于人阅读和编写,同时也易于机器解析和生成。`net.sf.json`是开源Java库,它...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于Web应用程序之间传递数据。它基于JavaScript的一个子集,具有易读易写的特点,同时也易于机器解析和生成。JSON格式通常由键值对组成,键...
jsonview是chrome浏览器的一个插件,用来在浏览器中查看json数据。比如你在浏览器中可以查看从服务器端传回来的json数据,这些数据可能没有经过格式化的,也或者是经过了unicode编码,没有缩进,没有换行等等,造成...
C# json格式转换,Json格式字符串与C#对象相互转换,类库和测试demo 写了一个json与C#对象相互装换的类库,直接调用就行,有测试案例,代码注释非常详细 部分方法: /// 将Json字符串解析为C#中的对象 /// Json格式...
可能包含的方法有`toJson()`(将Java对象转换为JSON字符串)、`fromJson()`(将JSON字符串解析为Java对象)、`convertToMap()`(将JSON字符串转换为Map)以及`convertToList()`(将JSON字符串转换为List)等。...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。在PHP中,JSON常用于与前端交互,传输数据。PHP 4.0版本虽然相对较旧,但仍然可以处理JSON数据,只是...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛应用于Web服务与客户端之间的数据传输。它易于人阅读和编写,同时也易于机器解析和生成。JSON文件通常以.js或.json为扩展名,其数据结构主要...
这篇关于“经典ASP读取JSON字符串/生成JSON对象,数组对象等”的知识将详细介绍如何在ASP环境中处理JSON数据。 1. **JSON对象与数组的结构**: JSON对象以大括号{}表示,键值对之间用逗号分隔。键必须是字符串,用...
**JsonSQL: SQL语句解析JSON文件** 在大数据处理和Web应用中,JSON(JavaScript Object Notation)格式已经成为数据交换的常见格式。然而,对于习惯使用SQL查询关系型数据库的人来说,处理JSON数据可能会觉得不太...
标题中的“PB解析json,解析JSON案例,解析jsondemo”表明了本文主要关注的是PowerBuilder(简称PB)如何处理JSON数据。在现代软件开发中,JSON(JavaScript Object Notation)是一种广泛使用的轻量级数据交换格式,...