如何将特定的JSONArray转换为JAVA对象?

时间:2017-10-24 10:00:11

标签: java json gson

在Java项目中,我使用WebSocket来获取订阅,我从socket获得了许多不同的响应JSONArray,我需要的响应如下所示:

[
  68,
  "te",
  [
    80588348,
    1508768162000,
    0.01569882,
    5700.8
  ]
]

应该如何看待此JAVA对象? 如何将其转换为此对象?

[
  68, <- Integer
  "te", <- String
  [
    80588348, <- Long
    1508768162000, <- Long
    0.01569882, <- Double
    5700.8 <- Double
  ]
]

有一个问题是还有其他答案,如:

{"event":"subscribed","channel":"trades","chanId":68,"symbol":"tBTCUSD","pair":"BTCUSD"}

当我尝试按new JSONArray(response)转换它时,它会抛出org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1].

如何获取和转换我需要的这些字段(第一个响应示例)?

我想得到这样的东西:

public class Details{
   public Long id;
   public Long timestamp;
   public Double amount;
   public Double price;
}

public class Response{
   public Integer id;
   public String type;
   public Details details;
}

2 个答案:

答案 0 :(得分:1)

如果您的回复采用固定格式,

示例:

JSONString : {"color":"yellow","type":"renault"}

在Java中,您可以使用以下代码:

Car car = objectMapper.readValue(jsonString, Car.class);  

您将Car类视为:

public class Car {

    private String color;
    private String type;

    // standard getters setters
}

答案 1 :(得分:1)

请求的解析器​​类:

public class JsonParser {
    public static Response toJavaObject(String str) {
        String[] fields = str.split(",");
        Response res = new Response();
        res.setId(Integer.valueOf(fields[0].substring(1)));
        res.setType(fields[1].replaceAll("\"", ""));
        Details dtl = new Details();
        dtl.setId(Long.valueOf(fields[2].substring(1)));
        dtl.setTimestamp(Long.valueOf(fields[3]));
        dtl.setAmount(Double.valueOf(fields[4]));
        dtl.setPrice(Double.valueOf(fields[5].substring(0, fields[5].length() - 2)));
        res.setDetails(dtl);

        return res;
    }
}

class Details {
    public Long id;
    public Long timestamp;
    public Double amount;
    public Double price;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

class Response {
    public Integer id;
    public String type;
    public Details details;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Details getDetails() {
        return details;
    }

    public void setDetails(Details details) {
        this.details = details;
    }
}

要使用此JsonParser,

例如在您的代码中:

public static void main(String args[]) {
    String str = "[68,\"te\",[80588348,1508768162000,0.01569882,5700.8]]";
    Response res = JsonParser.toJavaObject(str);
    // your logic below...
}