You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GSON API解析JSON,提取Resources数组中的JsonObject

获取SCIM JSON中Resources数组的JsonObject元素解决方案

Got it, let's walk through exactly how to pull each element from the Resources array as a com.google.gson.JsonObject—perfect for feeding into your existing method that requires this type. Here's a step-by-step solution tailored to your SCIM JSON response:

1. 确认Gson依赖已引入

First, make sure you have the latest stable Gson dependency in your project:

  • For Maven, add this to your pom.xml:
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version> <!-- Use the latest stable version available -->
</dependency>
  • For Gradle, add this to your build.gradle:
implementation 'com.google.code.gson:gson:2.10.1'

2. 核心代码实现

Here's the full code to parse your JSON string, extract the Resources array, and loop through each element as a JsonObject:

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ScimEntitlementProcessor {
    public static void main(String[] args) {
        // Your input JSON string (the one you provided)
        String scimJsonResponse = "{ \"totalResults\": 2, \"startIndex\": 1, \"schemas\": [ \"urn:ietf:params:scim:api:messages:2.0:ListResponse\" ], \"Resources\": [ { \"owner\": {}, \"application\": { \"displayName\": \"Non Auth App\", \"value\": \"4028b88168fa3d38016900d1759405a6\", \"$ref\": \"http://localhost:8080/identityiq/scim/v2/Applications/4028b88168fa3d38016900d1759405a6\" }, \"meta\": { \"created\": \"2019-03-22T18:40:30.505+05:30\", \"location\": \"http://localhost:8080/identityiq/scim/v2/Entitlements/297eff8a699f8b3a0169a5863769008b\", \"version\": \"W/\\\"1553260230505\\\"\", \"resourceType\": \"Entitlement\" }, \"schemas\": [ \"urn:ietf:params:scim:schemas:sailpoint:1.0:Entitlement\" ], \"displayableName\": \"reader\", \"aggregated\": false, \"id\": \"297eff8a699f8b3a0169a5863769008b\", \"requestable\": true, \"attribute\": \"Privileges\", \"type\": \"Entitlement\", \"descriptions\": [], \"value\": \"reader\" }, { \"owner\": {}, \"application\": { \"displayName\": \"Non Auth App\", \"value\": \"4028b88168fa3d38016900d1759405a6\", \"$ref\": \"http://localhost:8080/identityiq/scim/v2/Applications/4028b88168fa3d38016900d1759405a6\" }, \"meta\": { \"created\": \"2019-03-18T19:11:47.912+05:30\", \"location\": \"http://localhost:8080/identityiq/scim/v2/Entitlements/297eff8a6990fc49016991096d08001e\", \"version\": \"W/\\\"1552916507912\\\"\", \"resourceType\": \"Entitlement\" }, \"schemas\": [ \"urn:ietf:params:scim:schemas:sailpoint:1.0:Entitlement\" ], \"displayableName\": \"admin\", \"aggregated\": false, \"id\": \"297eff8a6990fc49016991096d08001e\", \"requestable\": true, \"attribute\": \"Privileges\", \"type\": \"Entitlement\", \"descriptions\": [], \"value\": \"admin\" } ] }";

        // Step 1: Parse the JSON string into the root JsonObject
        JsonObject rootJsonObject = JsonParser.parseString(scimJsonResponse).getAsJsonObject();

        // Step 2: Extract the Resources array from the root object
        JsonArray resourcesArray = rootJsonObject.getAsJsonArray("Resources");

        // Step 3: Loop through each element in the array and cast to JsonObject
        for (int i = 0; i < resourcesArray.size(); i++) {
            // Each element in Resources is a JSON object, so safely cast to JsonObject
            JsonObject entitlementJsonObject = resourcesArray.get(i).getAsJsonObject();

            // Pass the JsonObject to your existing method here
            processEntitlement(entitlementJsonObject);
        }
    }

    // Example method that accepts a JsonObject (replace with your actual method)
    private static void processEntitlement(JsonObject entitlement) {
        // Extract example attributes to demonstrate usage
        String entitlementId = entitlement.get("id").getAsString();
        String displayName = entitlement.get("displayableName").getAsString();
        String appValue = entitlement.getAsJsonObject("application").get("value").getAsString();

        System.out.printf("Processed Entitlement: ID=%s, Name=%s, App ID=%s%n", 
                          entitlementId, displayName, appValue);
    }
}

3. 关键说明

  • 安全类型转换: 你的SCIM响应中Resources数组的每个元素都是JSON对象,因此getAsJsonObject()可以安全使用,不会出现类型不匹配问题。
  • 可扩展性: 循环可以处理任意数量的数组元素,不管是示例中的2个还是成百上千个。
  • 现代Gson用法: JsonParser.parseString()是Gson 2.8+版本推荐的解析方式,替代了已废弃的new JsonParser().parse()方法。

内容的提问来源于stack exchange,提问作者Barca Fan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:56:18