简体   繁体   English

JSONException无法转换为java.lang.Throwable

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

How do I handle this exception? 如何处理此异常? I am unable to resolve this. 我无法解决此问题。

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 Result: 1 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);
    }


}

} }

You have your catch blocks in the wrong order. 您的接挡块顺序错误。 More-derived exceptions must precede less-derived ones. 派生度较高的异常必须先于派生度较低的异常。

You cannot put catch block in this order.The catch block with JSONException must be put before catch block with Exception 您不能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);
        }

Hope this solves your problem. 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM