android - 如何在 json 响应中获取值
问题描述
我需要在响应 api 帖子中获取值元素,我以 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");
谢谢
解决方案
你可以试试
try {
JSONObject responseOBJ = new JSONObject(String.valueOf(response));
if(responseOBJ.has(responseOBJ.getString("Planta"))
{
String as = responseOBJ.getString("Planta");
}
} catch (JSONException e) {
e.printStackTrace();
}
推荐阅读
- python-3.x - 用户输入和条件语句以及代码执行延迟和循环
- javascript - 从文件系统创建对象树
- c++ - 如何返回在我的函数中创建的数组的 unique_ptr?
- html - 如何在不使用跨度的情况下让我的所有 h2 使用相同的类?我希望我所有的 h2 都使用 .fancy_underline 类
- c# - 如何将 X(:,J) 等 Fortran 子数组语法转换为 VB.NET 和 C#
- laravel - Supervisord & Laravel - has been attempted too many times or run too long
- python - 我的代码有什么问题?添加“不”后失败。采取的步骤'到代码中
- spring-boot - 如何将 Spring log4j 与 messages.properties 集成?
- selenium - Heroku 脚本托管
- ios - 使用 iCloud PHAsset 请求 AVAsset 返回一个没有 VideoTracks 的 AVAsset