• notice
  • Congratulations on the launch of the Sought Tech site

Solve the abnormal situation when the key-value pair does not exist from Map and JSONObject

1. No exception will be reported when fetching non-existent key-value pairs in Map

will just return null

@Test
    public void testMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("d", 10);
        Object b = map.get("c");
        System.out.println(b);
    }
// The print result is: null.

Conclusion: An exception will not be reported when a non-existent key-value pair is retrieved from the map, and null will be returned.


2. There are two cases when taking a non-existing key-value pair in JSONObject

When using the jSONObject.get(Object key) method and get(String key) to fetch a non-existing key-value pair, no exception will be reported, and null will be returned; but when using the jSONObject.getxxx(String key) method, a null pointer exception will be reported:

@Test
    public void testJSONObject() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("d", 10);
        JSONObject jSONObject = JSONObject.fromObject(map);
        int xx = jSONObject.getInt("d");
        System.out.println(xx);
        // When using the get(Object key) method to retrieve a non-existent key-value pair from jSONObject, no exception will be reported, and null will be returned
        Object y = jSONObject.get("dd");
        System.out.println(y);
        // Use the getxxx(String key) method to retrieve the non-existent key-value pair from jSONObject and report an exception
        String y2 = jSONObject.getString("dd");
        System.out.println(y2);
    }

When dealing with null, a null pointer exception will be reported:

@Test
    public void testNull() {
        Object o = null;
        System.out.println(o);
        String s = (String) o;
        System.out.println(s);
        // System.out.println(s.getClass());
        User u = (User) o;
        System.out.println(u);
        // System.out.println(u.getClass());
    }
// print the result:
//null
//null
//null

Analysis shows that no matter what type of null is, Object null or String null or other types of null, a null pointer exception will be reported when the getClass() method is called.

If it is Object null, it can be converted to other types of null, because other types are subclasses of Object.

But other types of null cannot be converted to types, unless they are types with a parent-child relationship.

For example, if you want to convert from String type to User type, there will be a compile error saying Cannot cast from String to User.


Traverse JSONObject to get key-value pairs

Encountered when looking at the code, traverse JSONObject, get key-value pairs

Map<String, String> map = new HashMap<>();
JSONObject jsonObject = new JSONObject(jsonStr);//String converted to JSONObject
//jsonStr, for example: "{"key":"value","key1":"value1"}"
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext) {
    String key = keys.next();//Get key
    String value = jsonObject.getString(key);//Get value
    map.put(key, value);
}


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+