import org.codehaus.jettison.mapped.Configuration; import org.codehaus.jettison.mapped.MappedNamespaceConvention; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; public class JettisonExample { public static void main(String[] args) throws JSONException { Person person = new Person("John", 25); Configuration config = new Configuration(); config.setJsonTypeForDates(false); config.setIgnoreNamespaces(true); MappedNamespaceConvention convention = new MappedNamespaceConvention(config); JSONObject jsonObject = new JSONObject(convention); jsonObject.put("name", person.getName()); jsonObject.put("age", person.getAge()); System.out.println(jsonObject.toString()); String jsonString = "{\"name\":\"Alice\",\"age\":30}"; JSONObject json = new JSONObject(jsonString); Person person2 = new Person(json.getString("name"), json.getInt("age")); System.out.println(person2.getName()); System.out.println(person2.getAge()); } static class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } }


上一篇:
下一篇:
切换中文