JSONException无法转换为java.lang.Throwable

时间:2014-11-11 04:28:53

标签: java exception exception-handling

如何处理此异常?我无法解决这个问题。

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


}

}

2 个答案:

答案 0 :(得分:0)

你的拦截障碍顺序错误。更多派生的例外必须先于较少派生的例外。

答案 1 :(得分:0)

你不能按此顺序放置catch块。JSONException的catch块必须放在catch Exception之前

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

希望这能解决你的问题。