我正在构建一个android应用程序,需要向返回json对象并使用Volley库的API发出请求。
我能够获得响应,但是由于某种原因,我无法遍历json对象以接收所有键值。
我已经尝试了for(int i = 0; i
private void jsonParse(){
String url = "https://api.myjson.com/bins/wo1xc";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = response.getJSONObject("exactMatchDomain");
JSONArray jsonArray = response.getJSONArray("suggestedDomains");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject suggestedDomains = jsonArray.getJSONObject(i);
String domain = suggestedDomains.getString("domain");
String listPrice = suggestedDomains.getString("listPrice");
mTextViewResult.append(domain + ", " + listPrice + "\n\n" );
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQue.add(request);
答案 0 :(得分:0)
您应该这样做:
JSONArray jsonArray = response.getJSONArray("suggestedDomains");
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject suggestedDomains = jsonArray.getJSONObject(i);
String domain = suggestedDomains.getString("domain");
String listPrice = suggestedDomains.getString("listPrice");
mTextViewResult.append(domain + ", " + listPrice + "\n\n" );
}
由于回应:
{
"exactMatchDomain": {
"available": true,
"id": "domain",
"domain": "hyuirhejkyuiys.com",
"productId": 101,
"listPrice": "$13.99",
"salePrice": "$13.99",
"extendedValidation": false,
"disclaimer": null
},
"suggestedDomains": [
{
"id": "domain",
"domain": "hyuirhejkyuiys.net",
"available": true,
"productId": 12001,
"listPrice": "$18.99",
"salePrice": "$18.99",
"extendedValidation": false,
"disclaimer": null
},
{
"id": "domain",
"domain": "hyuirhejkyuiys.org",
"available": true,
"productId": 12101,
"listPrice": "$19.99",
"salePrice": "$9.99",
"extendedValidation": false,
"disclaimer": null
},
{
"id": "domain",
"domain": "hyuirhejkyuiys.info",
"available": true,
"productId": 201,
"listPrice": "$21.99",
"salePrice": "$4.99",
"extendedValidation": false,
"disclaimer": null
},
{
"id": "domain",
"domain": "hyuirhejkyuiys.co",
"available": true,
"productId": 56401,
"listPrice": "$34.99",
"salePrice": "$9.99",
"extendedValidation": false,
"disclaimer": null
},
{
"id": "domain",
"domain": "thehyuirhejkyuiys.com",
"available": true,
"productId": 101,
"listPrice": "$13.99",
"salePrice": "$13.99",
"extendedValidation": false,
"disclaimer": null
}
],
"disclaimer": "Taxes and ICANN fee not included in price
displayed.\n*Additional charges and registration restrictions may apply."
}
我建议您使用Retrofit 2代替Volley。