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