后端怎么生成{ "success": true, "message": "", "code": 200, "result": [ { "title": "中式球桌", "items": [{ "id": 1, "tableCode": "bj001", "tableName": "中式001", "tableType": 1, "typeName": "中式球桌" }] }, { "title": "斯诺克球桌", "items": [{ "id": 168, "tableCode": "bj009", "tableName": "斯诺克001", "tableType": 3, "typeName": "斯诺克球桌" }] } ], "timestamp": 1685101250812 }这样的格式
时间: 2023-07-03 19:12:02 浏览: 157
这是一个标准的 JSON 格式,可以使用后端程序语言如 Java、Python、PHP 等来生成。通常我们可以先定义一个包含所有数据的对象,然后使用 JSON 序列化工具将其转换为 JSON 格式的字符串。例如,在 Java 中可以使用 Gson 库,示例代码如下:
```
List<Map<String, Object>> result = new ArrayList<>();
Map<String, Object> item1 = new HashMap<>();
item1.put("title", "中式球桌");
List<Map<String, Object>> items1 = new ArrayList<>();
Map<String, Object> table1 = new HashMap<>();
table1.put("id", 1);
table1.put("tableCode", "bj001");
table1.put("tableName", "中式001");
table1.put("tableType", 1);
table1.put("typeName", "中式球桌");
items1.add(table1);
item1.put("items", items1);
result.add(item1);
Map<String, Object> item2 = new HashMap<>();
item2.put("title", "斯诺克球桌");
List<Map<String, Object>> items2 = new ArrayList<>();
Map<String, Object> table2 = new HashMap<>();
table2.put("id", 168);
table2.put("tableCode", "bj009");
table2.put("tableName", "斯诺克001");
table2.put("tableType", 3);
table2.put("typeName", "斯诺克球桌");
items2.add(table2);
item2.put("items", items2);
result.add(item2);
Map<String, Object> data = new HashMap<>();
data.put("success", true);
data.put("message", "");
data.put("code", 200);
data.put("result", result);
data.put("timestamp", System.currentTimeMillis());
Gson gson = new Gson();
String jsonString = gson.toJson(data);
```
这段代码生成的 jsonString 就是你所需的 JSON 格式字符串。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)