(libgdx学習)libgdxにおけるJSON応用
6595 ワード
△公式文書にはまだ実装されていないクラスもあるはずですが、エンジンの作者は書くときに使っているようです..
一、初期化及び一般的なAPI
二、応用例
Person
PhoneNumber
MyGame(1)は主にObjetc------>JSON文字列をプレゼンテーションするプロセスに用いられる
MyGame(2)主にJSON----->Objectのプレゼンテーションに用いられるプロセス
四、ソースのダウンロード
最初のMyGame(1):http://download.csdn.net/detail/caihongshijie6/7055195
2番目のMyGame(2):http://download.csdn.net/detail/caihongshijie6/7055205
一、初期化及び一般的な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
2番目のMyGame(2):http://download.csdn.net/detail/caihongshijie6/7055205