(官方文档中有一些类应该还没有实现,但是引擎的作者貌似就在写的时候就用了。。)
一、初始化及常见的API
Json json = new Json();//Json对象的创建及初始化 json.toJson(person):将person对象序列化成json字符串 /** * 为Person中的名为numbers时的成员变量指定类型名为PhoneNumber */ // json.setElementType(Person.class, "numbers", PhoneNumber.class); /** * 如果在JSON中对类的写操作无法避免的时候,我们可以给类起一个别名 * * json.addClassTag("phoneNumber", PhoneNumber.class)在输出的时候给 * PhoneNumber类起了一个别名phoneNumber */ json.addClassTag("phoneNumber", PhoneNumber.class); /** * prettyPrint(person):格式化输出 */ System.out.println("------>>>" + json.prettyPrint(person)); /** // * json.fromJson(Person.class, jsonText):反序列化.及将json字符串转换成对象 // * // */ // Person person2 = json.fromJson(Person.class, jsonText);
二、应用举例
Person
package com.example.groupactiontest; import java.util.ArrayList; public class Person { private String name; private int age; private ArrayList numbers; public Person() { } 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 ArrayList getNumbers() { return numbers; } public void setNumbers(ArrayList numbers) { this.numbers = numbers; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", numbers=" + numbers + "]"; } }
PhoneNumber
package com.example.groupactiontest; public class PhoneNumber { private String name; private String number; public PhoneNumber() { // TODO Auto-generated constructor stub } public PhoneNumber(String name, String number) { super(); this.name = name; this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
MyGame(1)主要用于演示Objetc---->JSON字符串的过程
package com.example.groupactiontest; import java.util.ArrayList; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.Input.Peripheral; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.utils.Json; public class MyGame implements ApplicationListener { @Override public void create() { Person person = new Person(); person.setName("Nate"); person.setAge(31); ArrayList numbers = new ArrayList(); numbers.add(new PhoneNumber("home","206-555-1234")); numbers.add(new PhoneNumber("work","425-555-4321")); person.setNumbers(numbers); Json json = new Json();//Json对象的创建及初始化 /** * json.toJson(person):将person对象序列化成json字符串 */ // System.out.println( "-------->" + json.toJson(person)); /** * 为Person中的名为numbers时的成员变量指定类型名为PhoneNumber */ // json.setElementType(Person.class, "numbers", PhoneNumber.class); /** * 如果在JSON中对类的写操作无法避免的时候,我们可以给类起一个别名 * * json.addClassTag("phoneNumber", PhoneNumber.class)在输出的时候给 * PhoneNumber类起了一个别名phoneNumber */ json.addClassTag("phoneNumber", PhoneNumber.class); /** * prettyPrint(person):格式化输出 */ System.out.println("------>>>" + json.prettyPrint(person)); } @Override public void dispose() { // TODO Auto-generated method stub } @Override public void pause() { // TODO Auto-generated method stub } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } @Override public void resize(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } }
MyGame(2)主要用于演示JSON------>Object的过程
package com.example.groupactiontest; import java.util.ArrayList; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.JsonWriter; import com.badlogic.gdx.utils.JsonWriter.OutputType; public class MyGame implements ApplicationListener { @Override public void create() { Person person = new Person(); person.setName("Nate"); person.setAge(31); ArrayList numbers = new ArrayList(); numbers.add(new PhoneNumber("home","206-555-1234")); numbers.add(new PhoneNumber("work","425-555-4321")); person.setNumbers(numbers); // Json json = new Json(); // String jsonText = json.toJson(person); // System.out.println("<<<<---------"+jsonText); // // /** // * json.fromJson(Person.class, jsonText):反序列化.及将json字符串转换成对象 // * // */ // Person person2 = json.fromJson(Person.class, jsonText); // System.out.println("--------->>>>>"+ person2); // Json json = new Json(); // json.setOutputType(OutputType.minimal); // /** // * json.toJson(person, Object.class):将person对象以Object的类型转换成json字符串 // * // * 这时候打印出来的json字符串中就会有类型的说明 // * 如class:com.example.xxxx.Person之类的 // */ // String text = json.toJson(person, Object.class); // System.out.println(json.prettyPrint(text)); // Object person2 = json.fromJson(Object.class, text);//将text以Object的转成Person对象 /** * JsonValue还没有实现 */ // Json json = new Json(); // String text = json.toJson(person,Object.class); // JsonWriter json = new JsonWriter(writer) // json.writeObjectStart(); // json.writeValue("a", "1"); // json.writeValue("b","2"); // json.writeObjectEnd(); // // System.out.println( "hello-------->"+ json.toString()); } @Override public void dispose() { // TODO Auto-generated method stub } @Override public void pause() { // TODO Auto-generated method stub } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } @Override public void resize(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } }
四、源码下载
第一个MyGame(1):http://download.csdn.net/detail/caihongshijie6/7055195
第二个MyGame(2):http://download.csdn.net/detail/caihongshijie6/7055205