JSON Simple - example

I'am using JSON Simple (https://code.google.com/p/json-simple/downloads/list).

But I have a problem with this.

I'm creating something like this:

  public String showMe() {
        JSONParser parser = new JSONParser();

        try {
            URL oracle = new URL("https://danepubliczne.imgw.pl/api/data/synop");
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                JSONArray a = (JSONArray) parser.parse(inputLine);

                for (Object o : a) {

                    JSONObject tutorials = (JSONObject) o;

                  Long id = (Long) tutorials.get("id_stacji");
                 if (id == 12650) {
                       stacja = (String) tutorials.get("stacja");
                       temperatura = (String) tutorials.get("temperatura");
                    return stacja;
               }
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

But it's not working.

I need to get stacja and temperatura with id:11 https://danepubliczne.imgw.pl/api/data/synop API and show it in TextView (with this I have no problem). How should I do?

Please help me.

1 answer

  • answered 2018-01-15 11:52 joao86

    id_stacji is not a long, it is a String.

    So you should do

    Long id = Long.parseLong(tutorials.get("id_stacji"));