简体   繁体   中英

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

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();
                    }
                }
            }

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();
}
try {
            JSONObject jsonObject = new JSONObject(sb);
            String url = jsonObject.optJSONArray("LatestData").getJSONObject(0).optString("url");
        } catch (JSONException e) {
            e.printStackTrace();
        }

使用loadJSONArray尝试

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

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();
    }

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

使用jsonschema2pojo生成JSON数据的pojo类

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM