【问题标题】:JSON Simple - exampleJSON 简单 - 示例
【发布时间】:2018-01-14 13:07:33
【问题描述】:

我正在使用 JSON Simple (https://code.google.com/p/json-simple/downloads/list)。

但我有一个问题。

我正在创建这样的东西:

  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;
    }

但它不起作用。

我需要使用 id:11 https://danepubliczne.imgw.pl/api/data/synop API 获取 stacja 和 temperatura 并在 TextView 中显示(这样我没问题)。我该怎么办?

请帮帮我。

【问题讨论】:

  • 你能把问题出在哪里吗?编译输出是什么?
  • id_stacji 不是很长,它是一个字符串。所以你应该这样做Long id = Long.parseLong(tutorials.get("id_stacji"));
  • @joao86 thx,但不工作... Logcat 显示:“致命异常:主进程:pl.....,PID:4764 java.lang.RuntimeException:无法实例化活动 ComponentInfo{ pl..../pl.....pogoda}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'
  • @Chrisss 请将 Logcat 中的完整堆栈信息放入您的问题中
  • 对不起!它正在工作@joa86,这是我在我的应用程序的另一个地方的错。谢谢!

标签: java android json json-simple


【解决方案1】:

id_stacji 不是长的,它是一个字符串。

所以你应该这样做

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

【讨论】:

    猜你喜欢
    • 2011-08-10
    • 2012-05-19
    • 2011-06-26
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    相关资源
    最近更新 更多