首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

Json格式数据的生成跟工具类ExtHelper的使用

2012-09-15 
Json格式数据的生成和工具类ExtHelper的使用一、需要的jar包:commons-beanutils-1.7.0.jar、commons-collect

Json格式数据的生成和工具类ExtHelper的使用

一、需要的jar包:

commons-beanutils-1.7.0.jar、commons-collections-3.2.jar、commons-lang-2.3.jar、commons-logging-1.0.4.jar、ezmorph-1.0.4.jar、json-lib-2.4-jdk15.jar

?

二、javabean

Person.java

package com.leo.bean;

public class Person {private String name;private int age;private PhoneNumber homePhone;private PhoneNumber officePhone;public Person(String name, int age, PhoneNumber homePhone,PhoneNumber officePhone) {super();this.name = name;this.age = age;this.homePhone = homePhone;this.officePhone = officePhone;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public PhoneNumber getHomePhone() {return homePhone;}public void setHomePhone(PhoneNumber homePhone) {this.homePhone = homePhone;}public PhoneNumber getOfficePhone() {return officePhone;}public void setOfficePhone(PhoneNumber officePhone) {this.officePhone = officePhone;}}

?PhoneNumber.java

package com.leo.bean;public class PhoneNumber {private String type;private String number;public PhoneNumber(String type, String number) {super();this.type = type;this.number = number;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}}

?

三、工具类

TotalJson.java

package com.leo.bean;import java.util.List;public class TotalJson {private long results;private List items;public long getResults() {return results;}public void setResults(long results) {this.results = results;}public List getItems() {return items;}public void setItems(List items) {this.items = items;}}

?ExtHelper.java

package com.leo.bean;import java.util.List;import net.sf.json.JSONObject;public class ExtHelper {public static String getJsonFromList(long recordTotal, List beanList) {TotalJson total = new TotalJson();total.setResults(recordTotal);total.setItems(beanList);JSONObject jsonArray = JSONObject.fromObject(total);return jsonArray.toString();}}

?

四、测试类

JsonListTest.java

package com.leo.test;

import java.util.ArrayList;import java.util.List;import com.leo.bean.ExtHelper;import com.leo.bean.PhoneNumber;public class JsonListTest {public static void main(String[] args) {// 创建PhoneNumber对象homePhonePhoneNumber homePhone = new PhoneNumber("宅电", "123456");// 创建PhoneNumber对象officePhonePhoneNumber officePhone = new PhoneNumber("办公电话", "654321");List phoneList = new ArrayList();phoneList.add(homePhone);phoneList.add(officePhone);String json = ExtHelper.getJsonFromList(phoneList.size(), phoneList);System.out.println(json);}}

?JsonLibTest.java

package com.leo.test;import net.sf.json.JSONObject;import com.leo.bean.Person;import com.leo.bean.PhoneNumber;public class JsonLibTest {public static void main(String[] args) {PhoneNumber homePhone = new PhoneNumber("宅电", "123456");PhoneNumber officePhone = new PhoneNumber("办公电话", "654321");Person person = new Person("tom", 20, homePhone, officePhone);JSONObject json = JSONObject.fromObject(person);String jsonStr = json.toString();System.out.println(json);}}
?

热点排行
Bad Request.