[英]Java Parsing A Json File Line To Return Part Of The Line
I am having some trouble trying to figure out how to parse a line in a json file so that it only returns part of the line as a string. 我在尝试弄清楚如何解析json文件中的一行时遇到了一些麻烦,因此它仅将一部分行作为字符串返回。 I will illustrate below:
我将在下面说明:
public String GetDistance(String origin, String destination) throws MalformedURLException, IOException {
//URL url = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins" + origin + ",UK+destination=" + destination + ",UK&key=mykey");
URL url = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Cornwall,UK&destinations=London,UK&key=mykey");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
String line, outputString = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
if (line.contains("distance")) {
outputString = reader.readLine().trim();
return outputString;
}
}
return outputString;
}
What this function does is create a json file in my browser using Google Maps API: 该功能的作用是使用Google Maps API在浏览器中创建一个json文件:
{
"destination_addresses" : [ "London, UK" ],
"origin_addresses" : [ "Cornwall, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "284 mi",
"value" : 456443
},
"duration" : {
"text" : "4 hours 52 mins",
"value" : 17530
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
Currently the "outputString" returns the line: "text" : "284 mi". 当前,“ outputString”返回以下行:“ text”:“ 284 mi”。 However, the desired output is to just return the miles, "284".
但是,所需的输出只是返回里程“ 284”。
I know this is most likely a re post, however I have been searching around for a solution to this and have been unsuccessful in implementing something that works. 我知道这很可能是转贴,但是我一直在寻找解决方案,但未能成功实现某些功能。
Any help on this would be greatly appreciated, Cheers. 干杯,对此的任何帮助将不胜感激。
You can have 2 solutions: 您可以有2个解决方案:
Parse the JSON and treat it as an object and then just return the String you're looking for. 解析JSON并将其视为对象,然后仅返回您要查找的String。
Split the line as follows: 如下分割行:
outputString = reader.readLine().trim();
That line above returns "text" : "284 mi" 上面的那一行返回“ text”:“ 284 mi”
Then you need to split the line by :
: 然后,你需要通过分割线
:
:
outputString.split(":")
That should create an array with 2 Strings: "text"
and "284 mi"
那应该创建一个包含2个字符串的数组:
"text"
和"284 mi"
Then take the second String and split it by a space 然后取第二个String并用空格分开
and take the first String:
并采用第一个字符串:
outputString.split("\\\\s")
Now you have: "284
现在您有:
"284
Then just return it from subindex 1 till the end, see docs : 然后,将其从子索引1返回到最后,请参阅docs :
outputString.substring(1)
And then just put it all together: 然后将它们放在一起:
return outputString.split(":")[1].split("\\\\s")[0].substring(1)
I haven't tried above code but it should work. 我没有尝试上面的代码,但它应该工作。
BTW follow Java Naming Conventions BTW遵循Java命名约定
And use them consistently 并持续使用它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.