如何从 JSONArray 获取数据
How to get data from JSONArray
我是 android 编程的新手,我正在尝试从此 JSON
获取数据
{
"user": {
"username": "justin",
"private": false,
"name": "Justin Nemeth",
"vip": true,
"vip_ep": false,
"ids": {
"slug": "justin"
},
"gender": "male",
"age": 32,
"images": {
"avatar": {
"full":
"https://secure.gravatar.com/avatar/30c2f0dfbc39e48656f40498aa871e33?r=pg&s=256"
}
}
}
我只需要得到 "username"
和 "full"
我尝试使用 jsonObject.getString("username")
,但它没有用,我做了更多研究,我发现我应该使用 JSONArray
,但我试过了,它仍然对我不起作用.我知道我做错了什么,但我不知道它是什么。
这是我的代码
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
userDetailsUri,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject mainObject = new JSONObject(response);
JSONArray resArray = mainObject.getJSONArray("user");
for (int i = 0; i < resArray.length(); i++) {
JSONObject jsonObject = resArray.getJSONObject(i);
UserDetails userDetails = new UserDetails();
userDetails.setUsername(jsonObject.getString("username"));
userDetails.setProfile_image(jsonObject.getString("full"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("vkv", "getUserDetails() " + error.toString());
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + access_token);
headers.put("trakt-api-version", "2");
headers.put("trakt-api-key", client_id);
return headers;
}
};
requestQueue.add(jsonObjectRequest);
user
是 JsonObject 而不是 JsonArray,因此您可以像下面这样获取名称和完整信息
JSONObject mainObject = new JSONObject();
JSONObject jobjUser =mainObject.getJSONObject("user");
String username=jobjUser.getString("username");
JSONObject jobjImages=mainObject.getJSONObject("images");
JSONObject jobjAvatar=jobjImages.getJSONObject("avatar");
String full=jobjAvatar.getString("full");
虽然我使用上面的答案还是有错误,但是我以此为基础解决了我的问题。这是代码
JSONObject mainObject = new JSONObject(response.toString());
JSONObject jsonObject = mainObject.getJSONObject("user");
JSONObject object = jsonObject.getJSONObject("images");
JSONObject jsonObject1 = object.getJSONObject("avatar");
String image = jsonObject1.getString("full");
String username = jsonObject.getString("username");
我是 android 编程的新手,我正在尝试从此 JSON
获取数据{
"user": {
"username": "justin",
"private": false,
"name": "Justin Nemeth",
"vip": true,
"vip_ep": false,
"ids": {
"slug": "justin"
},
"gender": "male",
"age": 32,
"images": {
"avatar": {
"full":
"https://secure.gravatar.com/avatar/30c2f0dfbc39e48656f40498aa871e33?r=pg&s=256"
}
}
}
我只需要得到 "username"
和 "full"
我尝试使用 jsonObject.getString("username")
,但它没有用,我做了更多研究,我发现我应该使用 JSONArray
,但我试过了,它仍然对我不起作用.我知道我做错了什么,但我不知道它是什么。
这是我的代码
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
userDetailsUri,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject mainObject = new JSONObject(response);
JSONArray resArray = mainObject.getJSONArray("user");
for (int i = 0; i < resArray.length(); i++) {
JSONObject jsonObject = resArray.getJSONObject(i);
UserDetails userDetails = new UserDetails();
userDetails.setUsername(jsonObject.getString("username"));
userDetails.setProfile_image(jsonObject.getString("full"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("vkv", "getUserDetails() " + error.toString());
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + access_token);
headers.put("trakt-api-version", "2");
headers.put("trakt-api-key", client_id);
return headers;
}
};
requestQueue.add(jsonObjectRequest);
user
是 JsonObject 而不是 JsonArray,因此您可以像下面这样获取名称和完整信息
JSONObject mainObject = new JSONObject();
JSONObject jobjUser =mainObject.getJSONObject("user");
String username=jobjUser.getString("username");
JSONObject jobjImages=mainObject.getJSONObject("images");
JSONObject jobjAvatar=jobjImages.getJSONObject("avatar");
String full=jobjAvatar.getString("full");
虽然我使用上面的答案还是有错误,但是我以此为基础解决了我的问题。这是代码
JSONObject mainObject = new JSONObject(response.toString());
JSONObject jsonObject = mainObject.getJSONObject("user");
JSONObject object = jsonObject.getJSONObject("images");
JSONObject jsonObject1 = object.getJSONObject("avatar");
String image = jsonObject1.getString("full");
String username = jsonObject.getString("username");