簡體   English   中英

JSONException無法轉換為java.lang.Throwable

[英]JSONException cannot be converted to java.lang.Throwable

如何處理此異常? 我無法解決此問題。

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: org.json.JSONException cannot be converted to java.lang.Throwable
        at jsontocsv.test.main(test.java:23)

Java結果:1

public class test {
    private static String JSON_DATA="{\"business_id\": \"JwUE5GmEO-sH1FuwJgKBlQ\", \"full_address\": \"6162 US Highway 51\nDe Forest, WI 53532\", \"hours\": {}, \"open\": true, \"categories\": [\"Restaurants\"], \"city\": \"De Forest\", \"review_count\": 26, \"name\": \"Pine Cone Restaurant\", \"neighborhoods\": [], \"longitude\": -89.335843999999994, \"state\": \"WI\", \"stars\": 4.0, \"latitude\": 43.238892999999997, \"attributes\": {\"Take-out\": true, \"Good For\": {\"dessert\": false, \"latenight\": false, \"lunch\": true, \"dinner\": false, \"breakfast\": false, \"brunch\": false}, \"Caters\": false, \"Noise Level\": \"average\", \"Takes Reservations\": false, \"Delivery\": false, \"Ambience\": {\"romantic\": false, \"intimate\": false, \"touristy\": false, \"hipster\": false, \"divey\": false, \"classy\": false, \"trendy\": false, \"upscale\": false, \"casual\": false}, \"Parking\": {\"garage\": false, \"street\": false, \"validated\": false, \"lot\": true, \"valet\": false}, \"Has TV\": true, \"Outdoor Seating\": false, \"Attire\": \"casual\",\"Alcohol\": \"none\", \"Waiter Service\": true, \"Accepts Credit Cards\": true, \"Good for Kids\": true, \"Good For Groups\": true, \"Price Range\": 1}, \"type\": \"business\"}";

public static void main(String[] args)  {
    try {
        final JSONObject obj = new JSONObject(JSON_DATA);
        final JSONArray businessId = obj.getJSONArray("business_id");

        final int n = businessId.length();
        for (int i = 0; i < n; ++i) {
            final JSONObject person = businessId.getJSONObject(i);
            System.out.println(person.getInt("id"));
            System.out.println(person.getString("name"));
            System.out.println(person.getString("gender"));
            System.out.println(person.getDouble("latitude"));
            System.out.println(person.getDouble("longitude"));
        }   } catch (Exception ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    }


}

}

您的接擋塊順序錯誤。 派生度較高的異常必須先於派生度較低的異常。

您不能JSONException放置catch塊。必須將JSONException catch塊放在Exception catch塊之前

try {
            final JSONObject obj = new JSONObject(JSON_DATA);
            final JSONArray businessId = obj.getJSONArray("business_id");

            final int n = businessId.length();
            for (int i = 0; i < n; ++i) {
                final JSONObject person = businessId.getJSONObject(i);
                System.out.println(person.getInt("id"));
                System.out.println(person.getString("name"));
                System.out.println(person.getString("gender"));
                System.out.println(person.getDouble("latitude"));
                System.out.println(person.getDouble("longitude"));
            }   }  catch (JSONException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }

希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM