Berikut ini contoh lengkap cara mem-parsing konten Json. Contohnya mengambil statistik versi Android (ditemukan dari kode sumber Android Studio di sini , yang terhubung ke sini ).
Salin file "distributions.json" yang Anda dapatkan dari sana ke res / raw, sebagai cadangan.
build.gradle
implementation 'com.google.code.gson:gson:2.8.6'
nyata
<uses-permission android:name="android.permission.INTERNET" />
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null)
return
thread {
// https://cs.android.com/android/platform/superproject/+/studio-master-dev:tools/adt/idea/android/src/com/android/tools/idea/stats/DistributionService.java
var root: JsonArray
Log.d("AppLog", "loading...")
try {
HttpURLConnection.setFollowRedirects(true)
val statsUrl = "https://dl.google.com/android/studio/metadata/distributions.json" //just a string
val url = URL(statsUrl)
val request: HttpURLConnection = url.openConnection() as HttpURLConnection
request.connectTimeout = 3000
request.connect()
InputStreamReader(request.content as InputStream).use {
root = JsonParser.parseReader(it).asJsonArray
}
} catch (e: Exception) {
Log.d("AppLog", "error while loading from Internet, so using fallback")
e.printStackTrace()
InputStreamReader(resources.openRawResource(R.raw.distributions)).use {
root = JsonParser.parseReader(it).asJsonArray
}
}
val decimalFormat = DecimalFormat("0.00")
Log.d("AppLog", "result:")
root.forEach {
val androidVersionInfo = it.asJsonObject
val versionNickName = androidVersionInfo.get("name").asString
val versionName = androidVersionInfo.get("version").asString
val versionApiLevel = androidVersionInfo.get("apiLevel").asInt
val marketSharePercentage = androidVersionInfo.get("distributionPercentage").asFloat * 100f
Log.d("AppLog", "\"$versionNickName\" - $versionName - API$versionApiLevel - ${decimalFormat.format(marketSharePercentage)}%")
}
}
}
}
Sebagai alternatif dari ketergantungan, Anda juga dapat menggunakan ini sebagai gantinya:
InputStreamReader(request.content as InputStream).use {
val jsonArray = JSONArray(it.readText())
}
dan mundur:
InputStreamReader(resources.openRawResource(R.raw.distributions)).use {
val jsonArray = JSONArray(it.readText())
}
Hasil menjalankan ini:
loading...
result:
"Ice Cream Sandwich" - 4.0 - API15 - 0.20%
"Jelly Bean" - 4.1 - API16 - 0.60%
"Jelly Bean" - 4.2 - API17 - 0.80%
"Jelly Bean" - 4.3 - API18 - 0.30%
"KitKat" - 4.4 - API19 - 4.00%
"Lollipop" - 5.0 - API21 - 1.80%
"Lollipop" - 5.1 - API22 - 7.40%
"Marshmallow" - 6.0 - API23 - 11.20%
"Nougat" - 7.0 - API24 - 7.50%
"Nougat" - 7.1 - API25 - 5.40%
"Oreo" - 8.0 - API26 - 7.30%
"Oreo" - 8.1 - API27 - 14.00%
"Pie" - 9.0 - API28 - 31.30%
"Android 10" - 10.0 - API29 - 8.20%