我正在尝试解析此JSON,但未在日志中得到任何结果。 样本json:
{
"coach_details": {
"coach_id": "1556",
"academy_id": "1",
"username": "testcoach@gmail.com",
"first_name": "test",
"last_name": "test",
"gender": "female",
"email": "",
"mobile": "9874563210",
"middle_name": "",
"nick_name": "",
"state": "4"
},
"status": 1,
"message": "success"
}
我已经尝试过了:
@Override
public void onResponse(JSONObject response) {
if (response != null) {
Log.d(TAG, "Response:\t" + response.toString());
try {
JSONObject jsonObject = new JSONObject(response.toString());
JSONObject details = jsonObject.getJSONObject("coach_details");
coachId = details.getString("coach_id");
Log.d(TAG, "Coach id:\t" + coachId);
Log.d(TAG, "Details:\t" + details);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
有什么办法可以解析此响应以获取详细信息对象键?谢谢。
答案 0 :(得分:1)
我希望这对您有用。
尝试这样
try {
JSONObject jsonObject1 = new JSONObject(response.toString());
JSONObject jsonObject2 = jsonObject1.getJSONObject("coach_details");
String coach_id = jsonObject2.getString("coach_id");
String academy_id = jsonObject2.getString("academy_id");
String status = jsonObject1.getString("status");
String message = jsonObject1.getString("message");
Log.e("coach_id", coach_id);
Log.e("academy_id", academy_id);
Log.e("status", status);
Log.e("message", message);
} catch (JSONException e) {
e.printStackTrace();
}
请确保您收到了服务器提供的响应。
答案 1 :(得分:1)
我正在发布我尝试过的完整代码。 而且我可以看到所有日志。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://devsports.copycon.in/api/coach_login";
Map<String, String> params = new HashMap();
params.put("username", "testcoach@gmail.com");
params.put("password", "123@abcd");
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
Log.d(TAG, "Response:\t" + response.toString());
try {
JSONObject jsonObject = new JSONObject(response.toString());
JSONObject details = jsonObject.getJSONObject("coach_details");
String coachId = details.getString("coach_id");
Log.d(TAG, "Coach id:\t" + coachId);
Log.d(TAG, "Details:\t" + details);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//TODO: handle failure
}
});
Volley.newRequestQueue(this).add(jsonRequest);
}
请检查完整的代码,并仔细检查日志。
如果可以,请尝试使用Gson库。
@Override
public void onResponse(JSONObject response) {
if (response != null) {
CoachResponse coachResponse = new Gson().fromJson(response.toString(), CoachResponse.class);
Log.d(TAG, "Coach id:\t" + coachResponse.getCoachDetails().getCoachId());
// Log.d(TAG, "Response:\t" + response.toString());
//
// try {
// JSONObject jsonObject = new JSONObject(response.toString());
// JSONObject details = jsonObject.getJSONObject("coach_details");
// String coachId = details.getString("coach_id");
// Log.d(TAG, "Coach id:\t" + coachId);
// Log.d(TAG, "Details:\t" + details);
// } catch (JSONException e) {
// e.printStackTrace();
// }
}
}
考虑映射类如下:
public class CoachResponse {
@SerializedName("coach_details")
@Expose
private CoachDetails coachDetails;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("message")
@Expose
private String message;
public CoachDetails getCoachDetails() {
return coachDetails;
}
public void setCoachDetails(CoachDetails coachDetails) {
this.coachDetails = coachDetails;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public class CoachDetails {
@SerializedName("coach_id")
@Expose
private String coachId;
@SerializedName("academy_id")
@Expose
private String academyId;
@SerializedName("username")
@Expose
private String username;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("gender")
@Expose
private String gender;
@SerializedName("email")
@Expose
private String email;
@SerializedName("mobile")
@Expose
private String mobile;
@SerializedName("middle_name")
@Expose
private String middleName;
@SerializedName("nick_name")
@Expose
private String nickName;
@SerializedName("state")
@Expose
private String state;
public String getCoachId() {
return coachId;
}
public void setCoachId(String coachId) {
this.coachId = coachId;
}
public String getAcademyId() {
return academyId;
}
public void setAcademyId(String academyId) {
this.academyId = academyId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
}
答案 2 :(得分:0)
@Override
public void onResponse(JSONObject response) {
if (response != null) {
Log.d(TAG, "Response:\t" + response.toString());
try {
// response is already JSONObject.
// JSONObject jsonObject = new JSONObject(response.toString());
if (response.has("coach_details"){
JSONObject details = response.getJSONObject("coach_details");
if (details.has("coach_id"){
coachId = details.getString("coach_id");
Log.d(TAG, "Coach id:\t" + coachId);
Log.d(TAG, "Details:\t" + details);
} else {
Log.d(TAG, "It has Not coach_id data");
}
} else {
Log.d(TAG, "It has Not coach_details data");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}