How to parse a JSON Input stream

前端 未结 11 1918
野的像风
野的像风 2020-11-30 01:06

I am using java to call a url that returns a JSON object:

url = new URL(\"my URl\");
urlInputStream = url.openConnection().getInputStream();
<
11条回答
  •  暖寄归人
    2020-11-30 01:40

    Kotlin version with Gson

    to read the response JSON:

    val response = BufferedReader(
                       InputStreamReader(conn.inputStream, "UTF-8")
                   ).use { it.readText() }
    

    to parse response we can use Gson:

    val model = Gson().fromJson(response, YourModelClass::class.java)
    

提交回复
热议问题