NullPointer使用GSON实现Cryptsy API

时间:2014-02-09 21:52:51

标签: java json gson

我正在编写用于访问Cryptsy API的Java API。尝试反序列化特定的api调用时,我遇到了NullPointerException问题。

问题行出现在Adapter类中:

JsonObject orderbookObj = json.getAsJsonObject().getAsJsonObject("return");

API致电:http://pubapi.cryptsy.com/api.php?method=orderdata

JSON输出:

{
  "success":1,
  "return":
  {
    "ASC":
    {
      "marketid":"111",
      "label":"ASC\/LTC",
      "primaryname":"AsicCoin",
      "primarycode":"ASC",
      "secondaryname":"LiteCoin",
      "secondarycode":"LTC",
      "sellorders":
      [
        {
          "price":"0.00004197",
          "quantity":"1536.00000000",
          "total":"0.06446592"
        },...
      ],
      "buyorders":
      [
        {
          "price":"0.00003957",
          "quantity":"7147.96283059",
          "total":"0.28284489"
        },...
      ]
    },
    "CENT":
    {
      ....

所有序列化对象的基类

public class Result {
private int success;
private String error;

public Result() {
    error = "";
}

// Getters and Setters
}

本课程以特定价格持有有关买/卖的信息。

public class GeneralOrder {

private double price;
private double quantity;
private double total;

public GeneralOrder(){}

// Getters and Setters
}

这是包含与“ASC”或“CENT”

相关的所有信息的类
public class GeneralOrderbook {

private int marketid ;
private String label ;
private String primarycode ;
private String primaryname ;
private String secondarycode ;
private String secondaryname ; 

@SerializedName("sellorders")
private GeneralOrder[] sellorders;

@SerializedName("buyorders")
private GeneralOrder[] buyorders;

public GeneralOrderbook(){}

    // Getters and Setters
}

在反序列化

期间使用这些类旁边的类
public class GeneralMarketData{
private GeneralMarket[] markets;

public GeneralMarketData(){}

// Getters and Setters
}

public class GeneralOrderbookReturn extends Result{

@SerializedName("return")
private GeneralOrderbookData data;

public GeneralOrderbookReturn(){}

// Getters and Setters
}

提供自定义反序列化的类

public class GeneralOrderbookAdapter implements JsonDeserializer<GeneralOrderbookData> {

public GeneralOrderbookData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    GeneralOrderbookData god = new GeneralOrderbookData();
    JsonObject orderbookObj = json.getAsJsonObject().getAsJsonObject("return");
    List<GeneralOrderbook> orderbooks = new ArrayList<GeneralOrderbook>();
    Iterator<Entry<String, JsonElement>> itr = orderbookObj.entrySet().iterator();
    while(itr.hasNext())
    {
        GeneralOrderbook orderbook = context.deserialize(itr.next().getValue(), GeneralOrderbook.class);
        orderbooks.add(orderbook);
    }
    god.setOrderbook(orderbooks.toArray(new GeneralOrderbook[0]));
    return god;
}
}

1 个答案:

答案 0 :(得分:0)

return是返回的JSON对象中的顶级字段,您可以将其映射到Result类。鉴于此,Result类应如下所示:

public class Result {
    private int success;
    @SerializedName("return")
    private MyClassThatModelsReturn data;
    private String error;

    public Result() {
        error = "";
    }

    // Getters and Setters
}

在不了解您尝试建模的数据的情况下,是&#34; ASC&#34;,&#34; CENT&#34;等文字的自动收报机符号或什么?如果是这样,那么看一下你的课程就可以建模为:

public class Result {
    private int success;
    @SerializedName("return")
    private Map<String, GeneralOrderbook> data;
    private String error;

    // Don't initialize error in constructor, just check for null
    public boolean isError() {
        return error != null;
    }

    // Getters and Setters
}