我需要在response api post中获取value元素,我收到的答复是json,下面是示例:
public void postJsonToServer() throws JSONException {
JSONObject js = createJsonObject();
String url = "http://link/User";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(com.android.volley.Request.Method.POST, url, js, new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
String as = response.getString("Planta");
} catch (JSONException e) {
e.printStackTrace();
}
响应:
{"Codigo":200,"Mensaje":"Usuario validado","Nombre":"Luis Smith","ClaveNom":"9633","Planta":"SIN","Indicador":"","Listado":""}
如何获取Planta和Indicador元素?
下一行返回null,我不知道该怎么办,请帮帮我。
String as = response.getString("Planta");
谢谢
答案 0 :(得分:1)
您可以尝试
try {
JSONObject responseOBJ = new JSONObject(String.valueOf(response));
if(responseOBJ.has(responseOBJ.getString("Planta"))
{
String as = responseOBJ.getString("Planta");
}
} catch (JSONException e) {
e.printStackTrace();
}