繁体   English   中英

将 JSON 数组解析为 Object(嵌套 JSON)

[英]Parsing a JSON array into Object (Nested JSON)

我有一个这样的 JSON 文件 -

{
  "users": [
    {
      "displayName": "Amanda Polly",
      "givenName": "Amanda",
      "surname": "Polly",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "emailAddress",
          "issuerAssignedId": "amandapolly@gmail.com"
        }
      ],
      "extension_timezone": "PST",
      "extension_locale": "en-US",
      "extension_tenant": "EG1234"
    },
    {
      "displayName": "Lowa Doe",
      "givenName": "Lowa",
      "surname": "Doe",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "userName",
          "issuerAssignedId": "lowadow123"
        }
      ],
      "extension_timezone": "PST",
      "extension_locale": "en-US",
      "extension_tenant": "EG1234"
    }
   ]
}

如果您可以看到有 2 个用户(Amanda 和 Lowa)包裹在“users”数组中。 我解析了文件并将所有这些转换为一个字符串。

现在我正在尝试遍历所有字段,如 displayName、givenName、surname 等! 但是,如果您看到身份再次是“signInType”和“issuerAssignedId”的包装器。

我编写了一个遍历所有用户的代码,但我无法获得“identites”字段。 下面是我的代码:

这里 JSONObject 的参数是 jsonAsString(这是我的具有上述 JSON 的字符串),现在我创建了一个 JSONArray 并传递包装器“用户”。

下面的代码工作正常,但有人可以帮助迭代每个用户的“identites”字段。

JSONObject jsonObject = new JSONObject(jsonAsString);
org.json.JSONArray jsonArray = jsonObject.getJSONArray("users");

System.out.println(jsonArray.length());

for(int i = 0; i< jsonArray.length();i++)
{
        displayName = jsonArray.getJSONObject(i).getString("displayName");
        givenName = jsonArray.getJSONObject(i).getString("givenName");
        surname = jsonArray.getJSONObject(i).getString("surname");
        extension_user_type = jsonArray.getJSONObject(i).getString("extension_user_type");
        
        
        try
        {
            extension_timezone = jsonArray.getJSONObject(i).getString("extension_timezone");
            extension_locale = jsonArray.getJSONObject(i).getString("extension_locale");
            extension_tenant = jsonArray.getJSONObject(i).getString("extension_tenant");
        }
        catch(JSONException e)
        {
            System.out.println("\nSome attribute was not found!");
        }
        
        System.out.println("\ndisplayName : "+displayName);
        System.out.println("\ngivenName : "+givenName);
        System.out.println("\nsurname : "+surname);
        System.out.println("\nextension_user_type : "+extension_user_type);
        System.out.println("\nextension_timezone : "+extension_timezone);
        System.out.println("\nextension_locale : "+extension_locale);
        System.out.println("\nextension_tenant : "+extension_tenant);

使用以下代码:

JSONObject jsonObject = new JSONObject(jsonAsString); org.json.JSONArray jsonArray = jsonObject.getJSONArray("users");

System.out.println(jsonArray.length());

for(int i = 0; i< jsonArray.length();i++)
{
        displayName = jsonArray.getJSONObject(i).getString("displayName");
        givenName = jsonArray.getJSONObject(i).getString("givenName");
        surname = jsonArray.getJSONObject(i).getString("surname");
        extension_user_type = jsonArray.getJSONObject(i).getString("extension_user_type");

        org.json.JSONArray jsonIdentitiesArray = jsonArray.getJSONObject(i).getJSONArray("identities");
        for(int j = 0; j< jsonIdentitiesArray.length();j++)
        {
            String signInType=jsonIdentitiesArray.getJSONObject(j).getString("signInType");
             System.out.println("signInType : "+signInType);
            String issuerAssignedId=jsonIdentitiesArray.getJSONObject(j).getString("issuerAssignedId");
             System.out.println("issuerAssignedId : "+issuerAssignedId);
        }
        
        try
        {
            extension_timezone = jsonArray.getJSONObject(i).getString("extension_timezone");
            extension_locale = jsonArray.getJSONObject(i).getString("extension_locale");
            extension_tenant = jsonArray.getJSONObject(i).getString("extension_tenant");
        }
        catch(JSONException e)
        {
            System.out.println("\nSome attribute was not found!");
        }
        
        System.out.println("\ndisplayName : "+displayName);
        System.out.println("\ngivenName : "+givenName);
        System.out.println("\nsurname : "+surname);
        System.out.println("\nextension_user_type : "+extension_user_type);
        System.out.println("\nextension_timezone : "+extension_timezone);
        System.out.println("\nextension_locale : "+extension_locale);
        System.out.println("\nextension_tenant : "+extension_tenant);

我认为您可以通过对用户 json arr 执行相同的过程来实现这一点,对于每个用户使用 get 函数来获取身份 json 数组并为每个用户迭代它。

JSONObject currentUserJsonObj = (JSONObject)jsonArray.getJSONObject(i);

JSONArray idnts=(jsonArray)(currentUserJsonObj.get("identities"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM