/**
*
*/
/**
* @author HP-Developer
*
*/
package test9_12json;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class Test {
private static void setDataFormat2JAVA(){
//设定日期转换格式
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"}));
}
//json转成Object
public void testJsonToObj(){
String json = "{id:'1001',name:'张三',age:22}";
Person stu = null;
setDataFormat2JAVA();
JSONObject obj = JSONObject.fromObject(json);
stu = (Person)JSONObject.toBean(obj, Person.class);
System.out.println(stu);
}
//从一个JSON数组得到一个java对象数组
public void testJsonArrToArray(){
String jsonStus = "[{id:1,name:'jack',age:20},{id:2,name:'rose',age:20},{id:3,name:'admin',age:20}]";
JSONArray array = JSONArray.fromObject(jsonStus);
Person[] stu = new Person[array.size()];
for(int i = 0; i < array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
stu[i] = (Person)JSONObject.toBean(jsonObject, Person.class);
}
System.out.println(stu[0]);
System.out.println(stu[1]);
System.out.println(stu[2]);
//System.out.println(stu[3]); 会报错
}
//从一个JSON数组得到一个java集合
public void testJsonArrToList(){
String jsonStus = "[{id:1,name:'jack',age:20},{id:2,name:'rose',age:20},{id:3,name:'admin',age:20}]";
JSONArray array = JSONArray.fromObject(jsonStus);
List<Person> stu = new ArrayList<Person>();
for(int i = 0; i < array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
stu.add((Person)JSONObject.toBean(jsonObject, Person.class));
}
System.out.println(stu.get(0));
System.out.println(stu.get(1));
System.out.println(stu.get(2));
}
//从json数组中得到相应java数组
public void testArrayForJson(){
String jsonString = "['q','c','d']";
JSONArray jsonArray = JSONArray.fromObject(jsonString);
Object[] strs = jsonArray.toArray();
System.out.print(strs[0]);
System.out.print(strs[1]);
System.out.print(strs[2]);
}
//字符串转换成json
public void testJsonStrToJSON(){
String json = "['json','is','easy']";
JSONArray jsonArray = JSONArray.fromObject( json );
System.out.println( jsonArray );
// prints ["json","is","easy"]
}
//Map转换成json
public void testMapToJSON(){
Map map = new HashMap();
map.put( "name", "jack" );
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 );
}
//java对象转换为json格式
public void testObjToJson(){
JSONObject obj2=new JSONObject();
obj2.put("phone","123456");
obj2.put("zip","7890");
obj2.put("contact",obj2);
System.out.print(obj2);
}
//复合类型bean转成成json
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
public void testArrayToJSON(){
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject( boolArray );
System.out.println( jsonArray );
}
//Collection对象转换成JSON
public void testListToJSON(){
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray = JSONArray.fromObject( list );
System.out.println( jsonArray );
// prints ["first","second"]
}
//普通类型的json转换成对象
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);
}
}
private void assertEquals(Object object, Object property) {
// TODO Auto-generated method stub
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java源码学习阶段所有的源码 (351个子文件)
Test.class 6KB
EmployeeInformationPro.class 4KB
Test1File.class 3KB
JsonLib.class 2KB
Employee.class 2KB
TestEmployee.class 2KB
TestException.class 2KB
Test1Parent.class 2KB
TestFileinandout.class 2KB
StringTest.class 2KB
InterfaceTest_picturephone.class 2KB
Test1.class 1KB
InterfaceTest_cameraofSLR.class 1KB
JsonLib_javabean.class 1KB
Test3son.class 1KB
Test3_packet.class 1KB
MathTest.class 1KB
Test2nianjing.class 1KB
DivisionExceptiontest.class 1KB
Test4_Datatest.class 1KB
Test1.class 1KB
Animal.class 1KB
Test3.class 1KB
SoftwareTest.class 990B
MusicTest.class 958B
InterfaceTest_cameraofdigital.class 958B
Test2.class 956B
DivisionException.class 954B
CommonEmployee.class 947B
Director.class 929B
Manager.class 926B
Test3Animalhaozi.class 912B
MyBean.class 888B
Test3Animaldog.class 869B
InterfaceTest_usualphone.class 863B
Testnianjingtang.class 856B
Test3Animal.class 842B
Testnianjingsun.class 810B
InterfaceTest_picturephonemain.class 785B
InterfaceTest_cameraofSLRmain.class 784B
Person.class 782B
Testmain.class 730B
InterfaceTest_camera.class 727B
InterfaceTest_cameraofdigitalmain.class 708B
InterfaceTest_phone.class 681B
InterfaceTest_usualphonemain.class 679B
Dog.class 637B
Test3Animalhaozitestmain.class 558B
Test3Animaldogtestmain.class 546B
Test2nianjingoftang.class 538B
Test2nianjingofsun.class 532B
TestofSoftware.class 525B
Testofmusic.class 507B
DogTest.class 497B
Stringabout.class 483B
JsontoPerson.class 405B
Test2Person.class 399B
Division.class 396B
CheckString.class 316B
Test2Student.class 289B
Test.class 265B
Animal.class 259B
Test2.class 256B
Test3.class 256B
Test.class 253B
InterfaceTest.class 242B
package-info.class 126B
package-info.class 123B
package-info.class 122B
package-info.class 118B
.classpath 1KB
stylesheet.css 13KB
index-14.html 25KB
Employee.html 15KB
overview-tree.html 15KB
index-8.html 14KB
InterfaceTest_cameraofSLR.html 13KB
InterfaceTest_picturephone.html 13KB
Test.html 13KB
Test3son.html 13KB
EmployeeInformationPro.html 12KB
index-13.html 12KB
Director.html 11KB
Manager.html 11KB
InterfaceTest_cameraofdigital.html 11KB
CommonEmployee.html 11KB
InterfaceTest_camera.html 11KB
Test1Parent.html 11KB
MyBean.html 11KB
index-6.html 11KB
Test1.html 11KB
Person.html 10KB
Test2nianjing.html 10KB
InterfaceTest_usualphone.html 10KB
InterfaceTest_phone.html 10KB
Stringabout.html 10KB
SoftwareTest.html 9KB
TestEmployee.html 9KB
Testnianjingsun.html 9KB
Testnianjingtang.html 9KB
共 351 条
- 1
- 2
- 3
- 4
资源评论
苦思冥想行则将至
- 粉丝: 1w+
- 资源: 110
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功