我有使用import json
和使用的现有 Python 程序
json.load() json.loads()
读取 json 并将其以易于使用的可访问字典的代码呈现的方法。
我使用 pythonjson[name][] = value
构造来引用内存中的 json 数据,并使用相同类型的语法为 json 元素分配新值。
我想将 Python 代码迁移到 Kotlin 并寻找 JSON 库,这些库将做一些接近 Python json.load() 在 Kotlin 中所做的事情。
我确实使用了 Google,但没有找到任何东西,有很多 Kotlin/Java JSON 库,但我认为它们没有提供像 Python JSON 加载方法这样的东西。
我最好使用的选择库是什么?
这是 Python 代码详细信息,我想在 Kotlin 中做同样的事情并使用现有的 JSON 库。
import json
js = json.loads(response['Body'].read())
js文件的内容是这样的:
{
"environment": "production",
"postgres_host" : "pgstaging-prod.blah.blah.rds.amazonaws.com",
"postgres_port" : 5432,
"postgres_database" : "datawarehouse",
"postgres_user" : "pguser",
"postgres_password" : "!!!!!!",
"postgres_config_schema" : "etl_operations",
"postgres_validation_log_table_name" : "jobs_process_log",
"postgres_destination_table_name" : "myleads",
"debugFlag": true,
"configBucket": "csn-datalake",
"configFileName": "yurib_test/myleads.json",
"HighWaterMarkFileName": "yurib_test/dts_high_watermark.json",
"repartitionSourceTableName": "leads",
"repartitionSourceDbName": "datalake",
"sourceTablePartitionDateTime": "job_run_timestamp_utc",
"repartitionTargetTableName": "outleads",
"repartitionTargetDbName": "datalake_reports",
"repartitionTargetS3Location": "yurib_test/outleads",
"repartitionTargetColumnList": [
{"colName": "message_item_countrycode", "isDatePartition": false, "renameTo" : "message_countrycode"},
{"colName": "message_tenant_code", "isDatePartition": false, "renameTo" : "message_tenant"},
{"colName": "message_lastupdated", "isDatePartition": true, "renameTo" : "message_lastupdated"}
],
"repartitionLoadType": "incremental_data_load",
"repartition": 4,
"repartitionLoadTypeIncremental": "incremental_data_load",
"repartitionLoadTypeFullInitial": "full_data_load",
"countryCodeColName": "message_item_countrycode",
"tenantColName": "message_tenant_code",
"autoCreateCountryTenant": false,
"autoCreateCountry": true,
"autoCreateTenant": true,
"partitionDateDefault": "0000-00-00",
"partitionYearDefault": "0000",
"partitionMonthDefault": "00",
"partitionDayDefault": "00",
"countryCodeDefault": "AU",
"tenantDefault": "CARSALES",
"missingPartColDataValReplace": "MISSINGDATA",
"validateIncomingCountryTenant": true,
"datePartitionStyle": "ym",
"datePartitionStyleYearMonth": "ym",
"datePartitionStyleYearMonthDay": "ymd",
"propagateGlueJobRunGuid": false
}
这里是 Python 如何使用 [] 和 range 访问上面的 json 文档
print (js["repartitionLoadType"])
print (js['configBucket'], js['configFileName'], js['HighWaterMarkFileName'])
print (js['repartitionTargetTableName'], js['repartitionTargetS3Location'])
print (js['repartitionSourceTableName'], js['repartitionSourceDbName'])
print (js['repartitionTargetDbName'], js['repartitionLoadType'])
print (js['autoCreateCountry'], js['autoCreateTenant'], js['missingPartColDataValReplace'])
print (js['countryCodeColName'], js['tenantColName'], js['propagateGlueJobRunGuid'])
print (js['countryCodeDefault'], js['tenantDefault'], js['validateIncomingCountryTenant'], js['repartition'])
partition_dts = ""
# json array
for i in range(0, len(js['repartitionTargetColumnList'])):
if True == js['repartitionTargetColumnList'][i]['isDatePartition']:
partition_dts = "`" + js['repartitionTargetColumnList'][i]['colName'] + "`"
else:
js['repartitionTargetColumnList'][i]['colName'] = "new value replace/assign here"
continue
# to set/replace/assign any values above:
js["repartitionLoadType"] = "some new value"
我希望这能澄清我试图将我的 Python 代码迁移到 Kotlin 的方法。