{"result":{"total":38,"items":[{"changeTime":"2020-11-26"}]}} 我想要 38 用java代码
时间: 2023-09-03 10:09:32 浏览: 14
您可以使用以下Java代码获取结果中的38:
```
import com.google.gson.Gson;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String jsonString = "{\"result\":{\"total\":38,\"items\":[{\"changeTime\":\"2020-11-26\"}]}}";
Gson gson = new Gson();
Map<String, Object> result = gson.fromJson(jsonString, Map.class);
int total = (int) result.get("total");
System.out.println(total);
}
}
```
相关问题
{"result":{"total":38,"items":[{"changeTime":"2020-11-26"}]}} 我想要 38 用java代码 用JSONObject
您可以使用以下代码将JSON字符串转换为JSONObject并获取"total"的值:
```
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String jsonString = "{\"result\":{\"total\":38,\"items\":[{\"changeTime\":\"2020-11-26\"}]}}";
JSONObject jsonObject = new JSONObject(jsonString);
int total = jsonObject.getJSONObject("result").getInt("total");
System.out.println(total); // Output: 38
}
}
```
请确保已经添加了org.json库,否则代码将无法编译。
{"result":{"total":38,"items":[{"changeTime":"2020-11-26"}]}} 我想要 38
您可以使用以下Python代码来提取结果中的38:
```
import json
result = '{"result":{"total":38,"items":[{"changeTime":"2020-11-26"}]}}'
data = json.loads(result)
total = data['result']['total']
print(total) # 输出38
```
首先,我们将结果字符串转换为Python对象使用`json.loads()`方法。然后,我们可以通过键值对来获取`total`的值,即`data['result']['total']`。最后,我们将结果打印出来,即38。