File tree Expand file tree Collapse file tree
core/src/main/java/com/alibaba/fastjson2 Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments