How to get a specific json key value from post request response in Android?

时光怂恿深爱的人放手 提交于 2019-12-20 05:25:53

问题


I have following response from post request using HttpURLConnection:

Post Request Response:

{
    "LatestData": [{
        "ExtraData": null,
        "ID": 0,
        "season": false,
        "latest": 0,
        "url": "http://www.awebsite.com/images/12.jpg"
    }]
}

How to get value of URL? I tried following but Android Studio keeps giving me error:

String newURL = sb.getJSONObject("LatestData").getString("url");
String newURL = sb.getJSONArray("LatestData").getJSONObject(0).getString("url");

Android Studio Error:

error: cannot find symbol method getJSONObject(String)
error: cannot find symbol method getJSONArray(String)

could you guys help me obtain the value of url and let me know what libraries i need to import to android studio so getsonObject works ?Thanks

android code:

 if (myURLConnection.getResponseCode() == 200) {
                br = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream(), "utf-8"));
                while (true) {
                    line = br.readLine();
                    if (line != null) {
                        sb.append(line + "\n");


                        //String newURL = sb.getJSONObject("LatestData").getString("url");
                        String newURL =sb.getJSONArray("LatestData").getJSONObject(0).getString("url");
                        mWebView.loadUrl("javascript:MyFunction('" +newURL + "');");
                    } else {
                        br.close();
                        return sb.toString();
                    }
                }
            }

回答1:


You need to convert sb to a JSONObject to access properties:

JSONOjbect jsonResponse = new JSONObject(new String(sb));

and then:

try {
    JSONArray jsonArray = jsonResponse.getJSONArray("LatestData");
    if (jsonArray != null && jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject dataOjbect = jsonArray.getJSONObject(i);
            String url = dataOjbect.getString("url");
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}



回答2:


try {
            JSONObject jsonObject = new JSONObject(sb);
            String url = jsonObject.optJSONArray("LatestData").getJSONObject(0).optString("url");
        } catch (JSONException e) {
            e.printStackTrace();
        }



回答3:


Try it using loadJSONArray

String url = loadJSONArray(sb)..getJSONObject(0).getString("url");



回答4:


Your approach should be like this.

try {
        List<String> urlist = new ArrayList<>();
        JSONObject jsonObject = new JSONObject(sb.toString());
        JSONArray jsonArray =  jsonObject.getJSONArray("LatestData");
        for (int count = 0; count<jsonArray.length(); count++){
            JSONObject jsonInner =  jsonArray.getJSONObject(count);
            String url = jsonInner.getString("url");
            //store your url in some list
            urlist.add(url);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }



回答5:


Can you try this :

StringBuilder sb = new StringBuilder();
line = br.readLine();
while(line != null){
    sb.append(line);
}
// if it's fail here then this means there is an issue in parsing JSONObject
JSONObject jsonObj = new JSONObject(sb.toString());        
JSONArray latestData = jsonObj.getJSONArray("LatestData");
JSONObject jsonObject = latestData.getJSONObject(0);
String url = jsonObject.getString("url");

Also I'm highly recommending to use Retrofit with GSON .. you can follow this article : http://www.vogella.com/tutorials/Retrofit/article.html




回答6:


use jsonschema2pojo to generate pojo classes for a JSON data



来源:https://stackoverflow.com/questions/47687419/how-to-get-a-specific-json-key-value-from-post-request-response-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!