The difference between optString and getString methods in JSON
The optString method will return an empty string when the value in the corresponding key does not exist, but getString will throw a JSONException.
/** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary, or throws if no such mapping exists. * * @throws JSONException if no such mapping exists. */ public String getString(String name) throws JSONException { Object object = get(name); String result = (object); if (result == null) { throw (name, object, "String"); } return result; } /** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary, or the empty string if no such mapping exists. */ public String optString(String name) { return optString(name, ""); }
The above is the difference between optString and getString methods in JSON introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
Related Articles
Android TabLayout Custom styles and usage details
This article mainly introduces Android TabLayout custom style and usage detailed explanation. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.2022-09-09Detailed explanation of the solution to the multi-line failure of Android TextView attribute ellipsize
This article mainly introduces the solution to the failure of Android TextView attribute ellipsize multiple lines. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let's take a look with the editor2018-07-07Android surfaceView realizes video playback function
This article mainly introduces the function of android surfaceView to play videos in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.2020-05-05Simple code for Android to save Bitmap as PNG image file
This article mainly introduces the method of Android to save Bitmap as PNG image file. It is very good and has reference value. Friends who need it can refer to it.2017-08-08Detailed explanation of the process of using Jetpack Compose to achieve the effect of flipped cards
Jetpack Compose is a declarative UI toolkit based on Kotlin, which can easily create beautiful user interfaces. Use Compose’s animation API and drawable API to easily achieve the effect of flipping cards. By setting properties such as rotation angle and transparency, the card can be flipped along the Y axis and achieve page turn effect2023-05-05Android WebView implements the function of screenshots
This article mainly introduces in detail to you a way to implement Android screenshots, which have certain reference value. Interested friends can refer to it.2018-05-05Android Webview redirection problem solution
During Android development, children's shoes that have used WebView may inevitably encounter URL redirection problems. This article mainly introduces the solution to Android Webview redirection problem, which is very practical. Friends who need it can refer to it.2018-05-05Android realizes address book effect - get mobile phone number and name
This article mainly introduces the Android address book effect - obtaining related information about mobile phone numbers and names. Friends who need it can refer to it2016-03-03The difference between the two ways of starting Service (startservice and bindservice)
andrid service: two startup methods: the first type of startservice and the second type of bindservice. Do you know the difference between these two startup methods? Let’s learn it together with the editor below.2015-11-11Detailed explanation of the method of Android cold boot to realize the APP opening in seconds
This article introduces you to the method of Android cold start to open an APP in seconds. It is very practical for everyone to develop APPs in daily life. If you need it, you can refer to it.2016-08-08