,可以通过以下步骤实现:
以下是一个示例代码,使用Jackson库将只有“行”和“列”的JSON响应解析为POJO:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonParser {
public static void main(String[] args) {
String json = "{\"rows\": 3, \"columns\": 2, \"data\": [[1, 2], [3, 4], [5, 6]]}";
try {
ObjectMapper objectMapper = new ObjectMapper();
ResponsePOJO response = objectMapper.readValue(json, ResponsePOJO.class);
int rows = response.getRows();
int columns = response.getColumns();
int[][] data = response.getData();
System.out.println("Rows: " + rows);
System.out.println("Columns: " + columns);
System.out.println("Data:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ResponsePOJO {
private int rows;
private int columns;
private int[][] data;
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getColumns() {
return columns;
}
public void setColumns(int columns) {
this.columns = columns;
}
public int[][] getData() {
return data;
}
public void setData(int[][] data) {
this.data = data;
}
}
在上述示例代码中,首先定义了一个ResponsePOJO类来表示JSON响应的结构。然后使用ObjectMapper类的readValue方法将JSON字符串解析为ResponsePOJO对象。最后,通过ResponsePOJO对象的getter方法获取解析后的数据,并进行输出。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云