繁体   English   中英

如何获取 JSON 数据的特定部分并将其保存为字符串

[英]How do I get specific parts of JSON data and save it as a String

我想从这个 JSON 文件中获取一些数据并将其保存为 String 变量。 我正在使用 Google 距离矩阵 API,结果以 JSON 形式给出。 我想将输出的某些部分保存为 String 变量。

public static void main(String[] args) throws IOException {
GoogMatrixRequest request = new GoogMatrixRequest();
String url_request = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=n178je&destinations=n113ns&mode=driving&key=" + API_KEY;

String response = request.run(url_request);
System.out.println(response);

这就是输出,我想检索的内容在这里。

我想取“距离”、“值”(7593)并将其保存为字符串变量。 有人能告诉我我怎么能做到这一点吗?

首先,您的响应已经是一个字符串。 如果您尝试获取其中的一部分,它将解析一个字符串。 而且因为是json字符串,所以有很多json映射器jar包你可以试试,比如Gson、JacksonMapper、simpleJSON……

对于您的特定情况,我建议您查看https://github.com/googlemaps/google-maps-services-java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM