【问题标题】:json response iteration android studio/retrofitjson响应迭代android studio/retrofit
【发布时间】:2020-04-22 20:25:33
【问题描述】:

这可能非常简单,但我是新手,在 android studio 中做这件事。这个 JSON 解析让我大吃一惊。基本上我只需要这个数组中 userName 的值。我能够得到我需要遍历每个“所有者”并获取用户名并将其放入列表的响应。我不知道如何获取 userName 字段。用户名可以包含@,也可以不包含。任何帮助是极大的赞赏!

【问题讨论】:

    标签: java android arrays json


    【解决方案1】:

    在您提供的 JSON 层次结构照片中,根元素的名称没有出现,所以我在下面的脚本中假设它为“root_element”

    • 这里可以将整个 JSON 响应读取为JSONObject。
    • 因为这个JSONObject 有多个元素(如图 命名为 0, 1, 2..);然后你可以得到一个JSONArray 使用 getJSONArray() 方法。
    • 然后您可以循环访问此JSONArray 以从 (0, 1, 2...) 中获取特定元素
    • 之后,您可以使用 JSON 结构照片中的 owner 这个特定元素创建另一个 JSONObject。
    • 最后您可以使用getString() 读取此所有者的字符串
    String jsonResponse = ""; // Put the entire JSON response as a String 
    JSONObject root = new JSONObject(jsonResponse); // jsonResponse is the entire JSON string
    JSONArray rootArray = root.getJSONArray("root_element"); // your root element is not show in your provided photo
    
    for (int i = 0; i < rootArray.length(); i++) {
    
        JSONObject element = rootArray.getJSONObject(i);
        JSONObject owner = element.getJSONObject("owner");
        String email = owner.getString("email");
        String givenName = owner.getString("givenName");
        String id = owner.getString("id");
        String lastName = owner.getString("lastName");
        String realm = owner.getString("realm");
        String userName = owner.getString("userName");
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 GSON 库将 userListAccounts 转换为 ArrayList,您可以从中轻松获取每个元素的用户名。

      添加 GSON 依赖

      <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
      </dependency>
      

      为 userListAccounts 创建模型类(用于 Kotlin)

      data class UserAccount(
      val creationTime: Int,
      val deletionTime: Int,
      val owner: Owner,
      val status: String, // I assume the Datatype is string
      )
      
      data class Owner(
      val email: String,
      val givenName: String,
      val id: String,
      val lastName: String,
      val realm: String, // I assume the Datatype is string
      val userName: String
      )
      

      为 userListAccounts 创建模型类(用于 Java)

      class UserAccount {
      Integer creationTime;
      Integer deletionTime;
      Owner owner;
      String status;
      
      public UserAccount(
          Integer creationTime,
          Integer deletionTime,
          Owner owner,
          String status
      ) {
          this.creationTime = creationTime;
          this.deletionTime = deletionTime;
          this.owner = owner;
          this.status = status;
      }
      }
      
      class Owner {
          String email;
          String givenName;
          String id;
          String lastName;
          String realm;
          String userName;
      
          public Owner(
              String email,
              String givenName,
              String id,
              String lastName,
              String realm,
              String userName
          ) {
              this.email = email;
              this.givenName = givenName;
              this.id = id;
              this.lastName = lastName;
              this.realm = realm;
              this.userName = userName;
          }
      }
      

      使用 GSON 库将 JSON 转换为对象

      val gson = Gson();
      val userAccounts: ArrayList<UserAccount> = gson.fromJson(userListAccounts, UserAccount::class.java);
      

      使用此对象访问用户名字段

      val usernameList = ArrayList<String>()
      
      for (account in userAccounts)
        usernameList.add(account.owner.username)
      

      usernameList 将包含您的 JSON 响应中存在的所有用户名。

      【讨论】:

      • 感谢您的回复,我认为是 Kotlin?我在 android studio 中使用 java 不确定等价物是什么。我已经做得更远了,但现在不知道如何“使用”它...for (Account userListAccount : userList.getAccounts()) Log.i("Access", "UserNames from json;" +userListAccount.getOwner().getUserName()); 及其记录用户名...但不确定如何在对话框或 toast 列表中实际使用它。 2020-04-22 19:28:52.638 18757-18757/com.test.myandroid I/Access: UserNames from json;tester@bla.com
      • 编辑了答案以在 java 中包含模型类。我希望你能用java做剩下的事情。
      猜你喜欢
      • 1970-01-01
      • 2016-10-01
      • 2020-11-14
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多