How to parse a JSON Input stream

前端 未结 11 1913
野的像风
野的像风 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)
    
    0 讨论(0)
  • 2020-11-30 01:42
    {
        InputStream is = HTTPClient.get(url);
        InputStreamReader reader = new InputStreamReader(is);
        JSONTokener tokenizer = new JSONTokener(reader);
        JSONObject jsonObject = new JSONObject(tokenizer);
    }
    
    0 讨论(0)
  • 2020-11-30 01:49

    if you have JSON file you can set it on assets folder then call it using this code

    InputStream in = mResources.getAssets().open("fragrances.json"); 
    // where mResources object from Resources class
    
    0 讨论(0)
  • 2020-11-30 01:51

    use jackson to convert json input stream to the map or object http://jackson.codehaus.org/

    there are also some other usefull libraries for json, you can google: json java

    0 讨论(0)
  • 2020-11-30 01:51

    I suggest use javax.json.Json factory as less verbose possible solution:

    JsonObject json = Json.createReader(yourInputStream).readObject();
    

    Enjoy!

    0 讨论(0)
提交回复
热议问题