簡體   English   中英

json 響應迭代 android 工作室/改造

[英]json response iteration android studio/retrofit

這可能非常簡單,但我是新手,在 android 工作室做這個。 這個 JSON 解析讓我大吃一驚。 基本上我只需要這個數組中 userName 的值。 我能夠得到我需要遍歷每個“所有者”並獲取用戶名並將其放入列表的響應。 我不知道如何獲取 userName 字段。 用戶名可以包含@,也可以不包含。 任何幫助是極大的贊賞!

在此處輸入圖像描述

您可以使用 GSON 庫將userListAccounts轉換為 ArrayList ,從中您可以簡單地獲取每個元素的用戶名。

添加GSON依賴

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

為 userListAccounts 創建您的 Model class(對於 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 創建您的 Model class(對於 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 轉換為 Object

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

使用此 Object 訪問用戶名字段

val usernameList = ArrayList<String>()

for (account in userAccounts)
  usernameList.add(account.owner.username)

usernameList 將包含 JSON 響應中存在的所有用戶名。

在您提供的 JSON 層次結構照片中,根元素的名稱沒有出現,所以我在下面的腳本中假設它為“root_element”

  • 這里可以將整個 JSON 響應讀取為JSONObject
  • 由於這個JSONObject中有多個元素(在圖片中命名為 0、1、2..); 然后您可以使用getJSONArray()方法獲取JSONArray
  • 然后你可以遍歷這個JSONArray從 (0, 1, 2...)
  • 之后,您可以從此特定元素中獲取另一個JSONObject ,該元素在您的 JSON 結構照片中owner
  • 最后,您可以使用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");

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM