java - Android 从 Json 对象解析字符串值

标签 java android json vk

我想从ApiVk方法response.responseString获取数据

String jsonStr1 =response.responseString;
JSONArray arr = new JSONArray(jsonStr1);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("id");

我得到了 NPE 如何解析字符串以获得值“id”的数组

String jsonStr1 = "{\"response\":{\"count\":254,\"items\":[{\"last_name\":\"Екимов\",\"id\":325813465,\"first_name\":\"Артём\"},{\"last_name\":\"Виноградов\",\"id\":448209461,\"first_name\":\"Дэнчик\"},{\"last_name\":\"Κонстантинов\",\"id\":444441827,\"first_name\":\"Κонстантин\"}]}}";

最佳答案

对于这个 json:

{
  "response": {
      "count": 254,
      "items": [{
          "last_name": "Екимов",
          "id": 23892340,
          "first_name": "Артём"
      }, {
          "last_name": "Екимов",
          "id":  23892381,
          "first_name": "Артём"
      }, {
          "last_name": "Екимов",
          "id": 23828492,
          "first_name": "Артём"
      }]
  }
}

获取id的分析如下:

try {
        JSONObject jsonresponse = jsonResponse.getJSONObject("response");
        JSONArray getJsonArray = jsonresponse.getJSONArray("items");
        for(int i = 0; i<getJsonArray.length();i++){
            JSONObject jsonAux= (JSONObject) jsonArrayItems.get(i);
            String id = jsonAux.getString("id");
            Log.e("ID = ",id);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

希望对您有所帮助。

关于java - Android 从 Json 对象解析字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467642/

相关文章:

java - 如何从 Java webapp 在远程 Linux 机器上远程安装 Java + Tomcat?

java - HttpServer 不是 API 的 VSCode Java 访问限制错误

java - Eclipse JDT中ActiveEditor的UndoContext

android - 在后台使用旧 Activity 开始 Activity

android - 上传json数据时如何按百分比显示进度条状态?

java - 如何验证电子邮件是否已在 Firebase 上注册?

安卓 MVP : which layer should store context variable

c++ - 将 JSON 文档转换为 Parquet

java - 如何从 php 文件获取变量并在 Android Activity 中使用它?

android - setOnEditorActionListener 不适用于 Android Lollipop