How do I parse this particular json string with Gson in Java?
{"orders":[{"oid":"347","status":"1"},{"oid":"348","status":"1"}],"a":14.15,"b":0}
What is problematic is the orders list.
I suppose one has to use a "type token" parameter to Gson.parse(json,type-toke), but it is not clear to me how this can be done.
解决方案
You have to create java types that it can be mapped to. So, you would have something like this.
public class Result {
private List orders;
private Number a;
private Number b;
// getter and setter for orders, a, and b
}
public class Order {
private Number oid;
private Number status;
// getter and setter for oid and status
}
Then you can just do the parsing with something like
Result result = gson.fromJson( yourSring, Result.class );
caveat, this is uncompiled, untested code, but should get you close.