【问题标题】:Parsing a JSON array into Object (Nested JSON)将 JSON 数组解析为对象(嵌套 JSON)
【发布时间】:2020-08-24 14:07:45
【问题描述】:

我有一个这样的 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 等等!但如果您看到 identities 又是“signInType”和“issuerAssignedId”的包装器。

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

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

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

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);

【问题讨论】:

  • 将你的 JSON 放入 this site 以将其转换为适当的 C# 数据类,然后只需使用 Root deserialized = JsonConvert.DeserializeObject(myJson); 反序列化整个事情
  • 你能推荐另一种方法吗?
  • 和我的一样吗?
  • 我可以。但是我提出的方法要容易得多。
  • 我实际上想以类似的方式进行,但由于限制,我不能。但是下次我肯定会使用这种方式,因为这是一种更好的方法

标签: java arrays json


【解决方案1】:

使用下面的代码:

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);

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2020-11-15
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多