使用 Java 到 json 的复杂键值对字符串
Complex keyValue pair String to json using Java
下面是我的键值对字符串。我尝试了很多方法但无法解决。在这里我不能用逗号分隔或用其他字符串替换。我从下面的代码和库中得到的结果是:
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
ResultSet rs = client.submit(queryString);
List<Result> results = rs.all().get();
System.out.println(results.toString());
[result{object=path[{id=User6, label=User, ORIGIN=AWS, ACCOUNT_ID=196000000000, TENANT_ID=sigma, ORG_ID=379000000000, USER_NAME=BritTest1, USER_ID=AIDAS3L75VZIP4F7ROFS6, USER_ARN=arn:aws:iam::196226166352:user/BritTest1, sfid=6, STATUS=Ready, CREATED_AT=Tue Nov 24 05:30:00 IST 2020, UPDATED_AT=Tue Nov 24 05:30:00 IST 2020}, {id=Rel26, label=User to Policy, IN={id=Policy56, label=Policy}, OUT={id=User6, label=User}, ORIGIN=AWS, ACCOUNT_ID=196226000000.00, TENANT_ID=sigma, ORG_ID=378564000000.00, STATUS=Ready, CREATED_AT=Wed Nov 25 05:30:00 IST 2020, UPDATED_AT=Wed Nov 25 05:30:00 IST 2020}, {id=Policy56, label=Policy, ORIGIN=AWS, ACCOUNT_ID=196226000000, TENANT_ID=sigma, ORG_ID=378564000000, STATUS=Ready, CREATED_AT=Wed Oct 28 05:30:00 IST 2020, UPDATED_AT=Wed Oct 28 05:30:00 IST 2020, POLICY_NAME=BritReadS3Policy, POLICY_ID=ANPAS3L75VZIGNCUV274M, POLICY_ARN=arn:aws:iam::196226166352:policy/BritReadS3Policy, POLICY_DOCUMENT={"Statement":[{"Action":["s3:GetBucketPolicyStatus","s3:GetBucketPublicAccessBlock","s3:GetLifecycleConfiguration","s3:GetBucketTagging","s3:GetInventoryConfiguration","s3:GetBucketWebsite","s3:GetBucketLogging","s3:ListBucket","s3:GetAccelerateConfiguration","s3:GetBucketVersioning","s3:GetBucketAcl","s3:GetBucketNotification","s3:GetBucketPolicy","s3:GetBucketObjectLockConfiguration","s3:GetEncryptionConfiguration","s3:GetIntelligentTieringConfiguration","s3:GetBucketRequestPayment","s3:GetBucketCORS","s3:GetAnalyticsConfiguration","s3:GetMetricsConfiguration","s3:GetBucketOwnershipControls","s3:GetBucketLocation"],"Effect":"Allow","Resource":"arn:aws:s3:::testbritive","Sid":"VisualEditor0"},{"Action":"s3:ListAllMyBuckets","Effect":"Allow","Resource":"*","Sid":"VisualEditor1"}],"Version":"2012-10-17"}, SF_ID=56, POLICY_TYPE=Customer-Managed}] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedPath}, result{object=path[{id=User6, label=User, ORIGIN=AWS, ACCOUNT_ID=196000000000, TENANT_ID=sigma, ORG_ID=379000000000, USER_NAME=BritTest1, USER_ID=AIDAS3L75VZIP4F7ROFS6, USER_ARN=arn:aws:iam::196226166352:user/BritTest1, sfid=6, STATUS=Ready, CREATED_AT=Tue Nov 24 05:30:00 IST 2020, UPDATED_AT=Tue Nov 24 05:30:00 IST 2020}, {id=Rel1066, label=User to Policy, IN={id=Policy138, label=Policy}, OUT={id=User6, label=User}, ORIGIN=AWS, ACCOUNT_ID=196226000000.00, TENANT_ID=sigma, ORG_ID=378564000000.00, STATUS=Ready, CREATED_AT=Wed Nov 25 05:30:00 IST 2020, UPDATED_AT=Wed Nov 25 05:30:00 IST 2020}, {id=Policy138, label=Policy, ORIGIN=AWS, ACCOUNT_ID=546836000000, TENANT_ID=sigma, ORG_ID=378564000000, STATUS=Ready, CREATED_AT=Wed Oct 28 05:30:00 IST 2020, UPDATED_AT=Wed Oct 28 05:30:00 IST 2020, POLICY_NAME=IAMUserChangePassword, POLICY_ID=NA, POLICY_ARN=arn:aws:iam::aws:policy/IAMUserChangePassword, POLICY_DOCUMENT={"Statement":[{"Action":["iam:ChangePassword"],"Effect":"Allow","Resource":["arn:aws:iam::*:user/${aws:username}"]},{"Action":["iam:GetAccountPasswordPolicy"],"Effect":"Allow","Resource":"*"}],"Version":"2012-10-17"}, SF_ID=138, POLICY_TYPE=AWS-Managed}] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedPath}]
您可以使用 ObjectMapper 进行此类转换
添加这些依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
然后尝试以下...
ObjectMapper objectMapper = newObjectMapper();
try{
String json = objectMapper.writeValueAsString(mapYouWantToConvert);
System.out.println(json);
} catch(JsonProcessingException e){
e.printStackTrace();
}
下面是我的键值对字符串。我尝试了很多方法但无法解决。在这里我不能用逗号分隔或用其他字符串替换。我从下面的代码和库中得到的结果是:
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
ResultSet rs = client.submit(queryString);
List<Result> results = rs.all().get();
System.out.println(results.toString());
[result{object=path[{id=User6, label=User, ORIGIN=AWS, ACCOUNT_ID=196000000000, TENANT_ID=sigma, ORG_ID=379000000000, USER_NAME=BritTest1, USER_ID=AIDAS3L75VZIP4F7ROFS6, USER_ARN=arn:aws:iam::196226166352:user/BritTest1, sfid=6, STATUS=Ready, CREATED_AT=Tue Nov 24 05:30:00 IST 2020, UPDATED_AT=Tue Nov 24 05:30:00 IST 2020}, {id=Rel26, label=User to Policy, IN={id=Policy56, label=Policy}, OUT={id=User6, label=User}, ORIGIN=AWS, ACCOUNT_ID=196226000000.00, TENANT_ID=sigma, ORG_ID=378564000000.00, STATUS=Ready, CREATED_AT=Wed Nov 25 05:30:00 IST 2020, UPDATED_AT=Wed Nov 25 05:30:00 IST 2020}, {id=Policy56, label=Policy, ORIGIN=AWS, ACCOUNT_ID=196226000000, TENANT_ID=sigma, ORG_ID=378564000000, STATUS=Ready, CREATED_AT=Wed Oct 28 05:30:00 IST 2020, UPDATED_AT=Wed Oct 28 05:30:00 IST 2020, POLICY_NAME=BritReadS3Policy, POLICY_ID=ANPAS3L75VZIGNCUV274M, POLICY_ARN=arn:aws:iam::196226166352:policy/BritReadS3Policy, POLICY_DOCUMENT={"Statement":[{"Action":["s3:GetBucketPolicyStatus","s3:GetBucketPublicAccessBlock","s3:GetLifecycleConfiguration","s3:GetBucketTagging","s3:GetInventoryConfiguration","s3:GetBucketWebsite","s3:GetBucketLogging","s3:ListBucket","s3:GetAccelerateConfiguration","s3:GetBucketVersioning","s3:GetBucketAcl","s3:GetBucketNotification","s3:GetBucketPolicy","s3:GetBucketObjectLockConfiguration","s3:GetEncryptionConfiguration","s3:GetIntelligentTieringConfiguration","s3:GetBucketRequestPayment","s3:GetBucketCORS","s3:GetAnalyticsConfiguration","s3:GetMetricsConfiguration","s3:GetBucketOwnershipControls","s3:GetBucketLocation"],"Effect":"Allow","Resource":"arn:aws:s3:::testbritive","Sid":"VisualEditor0"},{"Action":"s3:ListAllMyBuckets","Effect":"Allow","Resource":"*","Sid":"VisualEditor1"}],"Version":"2012-10-17"}, SF_ID=56, POLICY_TYPE=Customer-Managed}] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedPath}, result{object=path[{id=User6, label=User, ORIGIN=AWS, ACCOUNT_ID=196000000000, TENANT_ID=sigma, ORG_ID=379000000000, USER_NAME=BritTest1, USER_ID=AIDAS3L75VZIP4F7ROFS6, USER_ARN=arn:aws:iam::196226166352:user/BritTest1, sfid=6, STATUS=Ready, CREATED_AT=Tue Nov 24 05:30:00 IST 2020, UPDATED_AT=Tue Nov 24 05:30:00 IST 2020}, {id=Rel1066, label=User to Policy, IN={id=Policy138, label=Policy}, OUT={id=User6, label=User}, ORIGIN=AWS, ACCOUNT_ID=196226000000.00, TENANT_ID=sigma, ORG_ID=378564000000.00, STATUS=Ready, CREATED_AT=Wed Nov 25 05:30:00 IST 2020, UPDATED_AT=Wed Nov 25 05:30:00 IST 2020}, {id=Policy138, label=Policy, ORIGIN=AWS, ACCOUNT_ID=546836000000, TENANT_ID=sigma, ORG_ID=378564000000, STATUS=Ready, CREATED_AT=Wed Oct 28 05:30:00 IST 2020, UPDATED_AT=Wed Oct 28 05:30:00 IST 2020, POLICY_NAME=IAMUserChangePassword, POLICY_ID=NA, POLICY_ARN=arn:aws:iam::aws:policy/IAMUserChangePassword, POLICY_DOCUMENT={"Statement":[{"Action":["iam:ChangePassword"],"Effect":"Allow","Resource":["arn:aws:iam::*:user/${aws:username}"]},{"Action":["iam:GetAccountPasswordPolicy"],"Effect":"Allow","Resource":"*"}],"Version":"2012-10-17"}, SF_ID=138, POLICY_TYPE=AWS-Managed}] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedPath}]
您可以使用 ObjectMapper 进行此类转换 添加这些依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
然后尝试以下...
ObjectMapper objectMapper = newObjectMapper();
try{
String json = objectMapper.writeValueAsString(mapYouWantToConvert);
System.out.println(json);
} catch(JsonProcessingException e){
e.printStackTrace();
}