import org.json.simple.JSONArray;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class JsonTest implements JSONAware {
private final int x, y;
public JsonTest(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toJSONString() {
JSONArray arr = new JSONArray();
arr.add(this.x);
arr.add(this.y);
return arr.toString();
}
public static void main(String[] args) {
JsonTest jtest = new JsonTest(4, 5);
String test1 = JSONValue.toJSONString(jtest);
System.out.println(test1); //this works as expected
JSONObject obj = new JSONObject();
obj.put(jtest, "42");
System.out.println(obj); //this doesn't
}
}
出力として与える:
[4,5]
{"it.integrasistemi.scegliInPianta.etc.JsonTest@3cb89838":"42"}
それ以外の:
[4,5]
{[4,5]:"42"}
私は何が欠けていますか?
私の参照: http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_6-1_-_Customize_JSON_outputs