我遇到了问题,但不知道如何解决。
我有以下代码:
public static void main(String[] args)
{
BufferedReader br = null;
StringBuilder sb = null;
String line = null;
Scanner scanner = new Scanner(System.in);
//print the menu
System.out.println("Choose from the menu:");
System.out.println("1 -> Town weather ");
System.out.println("2 -> About");
System.out.println("3 -> Exit");
try
{
//read from keyboard the value
//int choice = scanner.nextInt();
int choice = 1;
switch (choice)
{
case 1:
System.out.println("Give desired town:");
String town = str.nextLine();
URL json = new URL("http://api.openweathermap.org/data/2.5/weather?q=" + town + "&APPID=");
HttpURLConnection url = (HttpURLConnection) json.openConnection();
url.setRequestMethod("GET");
url.connect();
//read the data from url
br = new BufferedReader(new InputStreamReader(url.getInputStream()));
sb = new StringBuilder();
while ((line = br.readLine()) != null)
{
sb.append(line + '\n');
}
String txt = sb.toString();
break;
case 2:
break;
case 3:
System.exit(0);
}
}
catch (InputMismatchException i)
{
System.out.println("Wrong choice!");
}
catch (MalformedURLException m)
{
System.out.println("Wrong URL!");
}
catch (IOException io)
{
System.out.println("Wrong town! The url shows 404 not found");
}
catch (NullPointerException np)
{
System.out.println("Null exception!");
np.printStackTrace();
}
catch (Exception e) //catch all exception where not previous caught.
{
e.printStackTrace();
}
}
所以我将 json 数据放入 txt 变量中。问题是,我的项目需要将所有数据(或其中的一部分)显示为列表。我必须以人们可以阅读的方式展示它们。
我尝试了 pretty-print ,但我不想显示符号 { } , :
理想情况下,我想将这些数据存储到数据库中,然后在维护它们的同时显示其中的一些数据。第一步是将它们拆分为一个数组,仅包含字符串,不包含任何特殊字符。
有人可以帮我解决这个问题吗?我搜索了 stackoverflow,发现了很多回复,但没有一个对我有用。
编辑:我更新了代码,包含完整的主代码,下面您可以看到 json 响应:
{
"coord": {
"lon": -86.97,
"lat": 34.8
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 270.48,
"pressure": 1033,
"humidity": 30,
"temp_min": 270.15,
"temp_max": 271.15
},
"visibility": 16093,
"wind": {
"speed": 2.6,
"deg": 340
},
"clouds": {
"all": 1
},
"dt": 1514921700,
"sys": {
"type": 1,
"id": 226,
"message": 0.0021,
"country": "US",
"sunrise": 1514897741,
"sunset": 1514933354
},
"id": 4830668,
"name": "Athens",
"cod": 200
}
预先感谢您的帮助。
最佳答案
尝试下面的代码将您的响应转换为 json 对象,您可以将其转换为所需的格式或稍后保存到数据库
package test;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Test {
String url="your URL"
DefaultHttpClient httpClient = new DefaultHttpClient();
httpget = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpget);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//read response from body
ResponseHandler<String> handler = new BasicResponseHandler();
String respBody = handler.handleResponse(httpResponse);
if (respBody != null && !"".equals(respBody)) {
JsonObject responseJson = new
JsonParser().parse(respBody).getAsJsonObject();
}
}
}
关于java - 无法使用 java 显示来自 URL 的 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48066523/