json-lib教程(二)

Student 類

package com.bethecoder.tutorials.json_lib.common;

import java.util.Date;

public class Student {
  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;

  public Student() {
  }

  public Student(String firstName, String lastName, int age, String hobby,
      Date dob) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.hobby = hobby;
    this.dob = dob;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  public Date getDob() {
    return dob;
  }
  public void setDob(Date dob) {
    this.dob = dob;
  }

  public String toString() {
    return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", hobby = " + hobby +
      ", dob = " + dob +
      " ]";
  }
}

1.Bean to JSON

package com.bethecoder.tutorials.json_lib.tests;

import java.util.Date;

import net.sf.json.JSONObject;

import com.bethecoder.tutorials.json_lib.common.Student;

public class Bean2Json {

  /**
   * @param args
   */
  public static void main(String[] args) {
    Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(110, 4, 6));
    JSONObject obj = JSONObject.fromObject(stud);
    System.out.println(obj.toString(2)); //pretty print with indent
  }

}

運行結果:java

{
  "age": 2,

  "dob":   {
    "date": 6,
    "day": 4,
    "hours": 0,
    "minutes": 0,
    "month": 4,
    "seconds": 0,
    "time": 1273084200000,
    "timezoneOffset": -330,
    "year": 110
  },

  "firstName": "Sriram",
  "hobby": "Singing",
  "lastName": "Kasireddi"
}

2.Bean to JSON Exclude

package com.bethecoder.tutorials.json_lib.tests;

import java.util.Date;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.bethecoder.tutorials.json_lib.common.Student;

public class Bean2JsonExclude {

  /**
   * @param args
   */
  public static void main(String[] args) {
    Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(110, 4, 6));
    
    JsonConfig jsonConfig = new JsonConfig();  
    jsonConfig.setExcludes( new String[]{ "hobby", "dob" } ); 
    
    JSONObject obj = JSONObject.fromObject(stud, jsonConfig);
    System.out.println(obj.toString(2)); //pretty print with indent
  }

}

運行結果:json

{
  "age": 2,
  "firstName": "Sriram",
  "lastName": "Kasireddi"
}

###3.Bean to JSON Exclude Filterapp

package com.bethecoder.tutorials.json_lib.tests;

import java.util.Date;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import com.bethecoder.tutorials.json_lib.common.Student;

public class Bean2JsonExcludeFilter {

  /**
   * @param args
   */
  public static void main(String[] args) {
    Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(110, 4, 6));
    
    JsonConfig jsonConfig = new JsonConfig();  
    jsonConfig.setJsonPropertyFilter(new PrimitevePropertyFilter());
    
    JSONObject obj = JSONObject.fromObject(stud, jsonConfig);
    System.out.println(obj.toString(2)); //pretty print with indent
  }
}

class PrimitevePropertyFilter implements PropertyFilter {
  @Override
  public boolean apply(Object source, String name, Object value) {
    //System.out.println("name = " + name + ", value = " + value);
    if (Number.class.isAssignableFrom(value.getClass()) ||
      String.class.isAssignableFrom(value.getClass())  ) {
      
      return false;
    }
    return true;
  }
}

運行結果:ide

{
  "age": 2,
  "firstName": "Sriram",
  "hobby": "Singing",
  "lastName": "Kasireddi"
}
相關文章
相關標籤/搜索