Unhandled JSONException Type

JSONObject login = new JSONObject();
login.put("Key1", "Value1");

I was just trying to create a simple JSON object with key and value pairs. I get this exception " Unhandled exception type JSONException".

Map<String,String> map = new HashMap<String,String>
map.put("key1", "value1");

Are they the equivalent way to create an object with a key, a pair of values. Which is the preferred way when creating an object that needs to be sent to the service.

+5
source share
2 answers

Unhandled JSONException Type

You need to wrap your code in a try-catch block. This is your warning.

JSONObject login = new JSONObject();
    try {
        login.put("Key1", "Value1");
    } 
    catch (JSONException e) {... }

Are they the equivalent way to create an object with a key, a pair value. Which is the preferred way when creating the object that is needed to send to the service.

JSONObject.put()cancels JSONException and Map.put()no.

-, .

JSON - , , , .

Map ( ) , , KeySet() Map String (, StringBuilder), , JSON.

, " " , JSON .

+15

JSONObject:

  • JSON
  • , , JSONObject

  • , , Collection
+1

All Articles