Skip to content

Commit c8582b5

Browse files
jujnwenshao
authored andcommitted
fix issue #3880 (#3881)
* fix issue #3880 * update JSONArray
1 parent b03848f commit c8582b5

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

core/src/main/java/com/alibaba/fastjson2/JSONArray.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,22 @@ public JSONObject getJSONObject(int index) {
227227
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
228228
*/
229229
public String getString(int index) {
230+
return getString(index, null);
231+
}
232+
233+
/**
234+
* Returns the {@link String} at the specified location in this {@link JSONArray}.
235+
*
236+
* @param index index of the element to return
237+
* @param defaultValue the default mapping of the index
238+
* @return {@link String} or null
239+
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
240+
*/
241+
public String getString(int index, String defaultValue) {
230242
Object value = get(index);
231243

232244
if (value == null) {
233-
return null;
245+
return defaultValue;
234246
}
235247

236248
if (value instanceof String) {

core/src/main/java/com/alibaba/fastjson2/JSONObject.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,28 @@ public JSONObject getJSONObject(String key) {
325325

326326
return null;
327327
}
328-
329328
/**
330329
* Returns the {@link String} of the associated keys in this {@link JSONObject}.
331330
*
332331
* @param key the key whose associated value is to be returned
333332
* @return {@link String} or null
334333
*/
335334
public String getString(String key) {
335+
return getString(key, null);
336+
}
337+
338+
/**
339+
* Returns the {@link String} of the associated keys in this {@link JSONObject}.
340+
*
341+
* @param key the key whose associated value is to be returned
342+
* @param defaultValue the default mapping of the key
343+
* @return {@link String} or null
344+
*/
345+
public String getString(String key, String defaultValue) {
336346
Object value = super.get(key);
337347

338348
if (value == null) {
339-
return null;
349+
return defaultValue;
340350
}
341351

342352
if (value instanceof String) {

0 commit comments

Comments
 (0)