基于driverKey将字典列表拆分为多组独立列表的实现求助
driverKey It looks like you're overcomplicating things by focusing on the connectionString field—this task is purely about grouping entries based on their driverKey value, and the connection string doesn't affect the grouping logic at all. Here's a straightforward implementation using Python (which is great for JSON manipulation):
Step-by-Step Implementation
1. Parse the Input JSON
First, we'll load your input JSON into a Python dictionary to work with it easily.
2. Define Optional Key Mapping (If Needed)
Your expected output uses simplified keys like PostgreSQL instead of the full PostgreSQL Unicode from driverKey, and SQL SERVER instead of SQL Server. We can add a mapping dict to handle this conversion.
3. Group Entries by driverKey
Loop through each entry in the RDBMS list, use the driverKey (or mapped key) to group entries into a new dictionary of lists.
Full Code Example
import json # Your input JSON data input_json = ''' { "RDBMS": [ { "userName": "a", "entityName": "entity1", "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;", "databaseName": "database1", "driverKey": "PostgreSQL Unicode" }, { "userName": "b", "entityName": "entity2", "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;", "databaseName": "database1", "driverKey": "PostgreSQL Unicode" }, { "userName": "a", "entityName": "entity3", "connectionString": "DRIVER={SQL Server};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=1433;", "databaseName": "database1", "driverKey": "SQL Server" } ] } ''' # Parse JSON to Python dict data = json.loads(input_json) # Map driverKey values to your desired group names (adjust as needed) driver_key_mapping = { "PostgreSQL Unicode": "PostgreSQL", "SQL Server": "SQL SERVER" } # Initialize empty dict to hold grouped results grouped_data = {} # Iterate over each RDBMS entry for entry in data["RDBMS"]: # Get the original driver key original_key = entry["driverKey"] # Use mapped key if available, else keep original group_name = driver_key_mapping.get(original_key, original_key) # Create a new list for the group if it doesn't exist if group_name not in grouped_data: grouped_data[group_name] = [] # Add the current entry to the group grouped_data[group_name].append(entry) # Print the formatted result print(json.dumps(grouped_data, indent=4))
Output Result
Running this code will produce exactly the structure you're expecting (with the corrected SQL SERVER entry that matches your input's third item):
{ "PostgreSQL": [ { "userName": "a", "entityName": "entity1", "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;", "databaseName": "database1", "driverKey": "PostgreSQL Unicode" }, { "userName": "b", "entityName": "entity2", "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;", "databaseName": "database1", "driverKey": "PostgreSQL Unicode" } ], "SQL SERVER": [ { "userName": "a", "entityName": "entity3", "connectionString": "DRIVER={SQL Server};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=1433;", "databaseName": "database1", "driverKey": "SQL Server" } ] }
Why Your Previous Attempts Might Have Failed
Chances are you accidentally incorporated the connectionString into your grouping logic, or didn't properly initialize empty lists for new groups. The key here is to ignore all fields except driverKey when deciding which group an entry belongs to—everything else is just data to carry over into the grouped lists.
内容的提问来源于stack exchange,提问作者Roshan Bagdiya

