0

I'm developing a desktop application in Netbeans using Java Swing. I'm using a weather condition API to show weekly results. After parsing json file, the key "icon" is not displaying as an image in the text pane. Can someone explain me that how can I make that icons visible? Here is my sample code after requesting. (Can't show the connection code because of API KEY) I'm parsing the data :

try {  
      HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
      JsonParser jp = new JsonParser();
      JsonElement root = null;

        root = jp.parse(response.body());
        JsonObject jsonobj = root.getAsJsonObject();

        

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String req_result = gson.toJson(jsonobj.get("result"));
havayı_listele.setText(req_result.replaceAll("\\{", "").replaceAll("\\},", "").replaceAll("\"", "").replaceAll("\\}", "").replaceAll("\\[", "").replaceAll("\\]", ""));

//JSON FILE LOOKS LIKE THIS

{
  "result": [
    {
      "date": "24.09.2018",
      "day": "Pazartesi",
      "icon": "https://image.flaticon.com/icons/svg/143/143769.svg",
      "description": "açık",
      "status": "Clear",
      "degree": "31",
      "min": "11.6",
      "max": "31",
      "night": "11.6",
      "humidity": "17"
    },
    {
      "date": "25.09.2018",
      "day": "Salı",
      "icon": "https://image.flaticon.com/icons/svg/143/143769.svg",
      "description": "yağmurlu",
      "status": "Rainy",
      "degree": "24.14",
      "min": "7.63",
      "max": "25.82",
      "night": "9.09",
      "humidity": "35"
    },
    "..."
  ]
}

My textPane is look like this after some arrangements : https://i.stack.imgur.com/SAmPC.png

I wanna display the icon links as images.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    If that's what you *want*, why is there no code to get the URL from the JSON, code to download the image from that URL, and code to show the downloaded image in a position near the part of the JSON text where the image URL was given? --- *Side note:* Mass converting the JSON to plan text like you're doing will make it virtually impossible to place the images anywhere near where you presumably want them, so even the code you do have is doing it wrong. – Andreas Jan 17 '21 at 05:31
  • Thank you for the response. I actually want to show that data in a proper way. So I was using the plain text. Is there any possible way to show that API data in a better way? I guess it's not possible to place images in the required location. – Tolkienist Coder Jan 17 '21 at 06:00
  • 2
    *"svg icon"* Scalable Vector Graphics are great! Java offers no support for them. I wish they did. **Edit:** Maybe the Java-FX [`WebView`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/web/WebView.html) can handle (as in display) an SVG. – Andrew Thompson Jan 17 '21 at 06:40
  • @AndrewThompson Appreciated for the advice. I am a beginner in the programming and not familiar with json files. Well, if Java does not support the image icon, do you know how can I remove that "icon "key from my json file? – Tolkienist Coder Jan 17 '21 at 06:57
  • I've never worked with JSON. I'd expect an API that is focused on parsing JSON would make it simple. Having said that, why 'remove' the information rather than just ignore it in your code? – Andrew Thompson Jan 17 '21 at 07:09
  • 2
    Agreed with @AndrewThompson what you might want to do is have an html template that you parse the json data into, specifically the svg icon will be parse into a `` tag and then use a JavaFX webview to display the html. Here is an example of a javafx webview in swing https://stackoverflow.com/questions/13717769/jeditorpane-with-javascript-and-css-support/13718130#13718130 – David Kroukamp Jan 17 '21 at 07:16

0 Answers0