1.反序列化
[{"Datetime":"2022-05-28T08:34:15.000Z","HQ__c":["01"],"Office__c":["01"],"Store__c":["01B1"],"Rank__c":["S"]}, {"Datetime":"2022-08-31T12:17:52.000Z","HQ__c":["01","0A"],"Office__c":["01","0A"],"Store__c":["01B1","0AE0"],"Rank__c":["A","B"]}]
在Apex中使用deserializeUntyped方法反序列化处理,查看具体内容。
public with sharing class JsonParseringClass {
/**Opportunity Prefix*/
private static final String SOBJECT_TYPE_PREFIX_OPPORTUNITY
= Schema.SObjectType.Opportunity.getKeyPrefix();
public JsonParseringClass() {
Project__c projectItem = [SELECT Id,SYS_ApprovalJson__c FROM Project__c WHERE Id = 'a025g00000AJaWUAA1'];
if (projectItem.SYS_ApprovalJson__c != null && projectItem.SYS_ApprovalJson__c.startsWith('[') && projectItem.SYS_ApprovalJson__c.endsWith(']')) {
List<Object> useAppListJson = (List<Object>)JSON.deserializeUntyped(projectItem.SYS_ApprovalJson__c);
system.debug('>>>>>>>>>useAppListJson>>>>>'+useAppListJson);
if (useAppListJson != null) {
for (Object listItem : useAppListJson) {
if(listItem instanceof Map<String,Object>) {
Map<String,Object> mapItem = (Map<String,Object>)listItem;
system.debug('>>>>>>>>>mapItem>>>>>'+mapItem);
}
}
}
}
}
}
Log结果
USER_DEBUG [9]|DEBUG|>>>>>>>>>useAppListJson>>>>>
({Datetime=2022-05-28T08:34:15.000Z, HQ__c=(01),
Office__c=(01), Rank__c=(S), Store__c=(01B1)},
{Datetime=2022-08-31T12:17:52.000Z, HQ__c=(01, 0A),
Office__c=(01, 0A), Rank__c=(A, B), Store__c=(01B1, 0AE0)})
USER_DEBUG [15]|DEBUG|>>>>>>>>>mapItem>>>>>
{Datetime=2022-05-28T08:34:15.000Z, HQ__c=(01), Office__c=(01), Rank__c=(S), Store__c=(01B1)}
USER_DEBUG [15]|DEBUG|>>>>>>>>>mapItem>>>>>
{Datetime=2022-08-31T12:17:52.000Z, HQ__c=(01, 0A), Office__c=(01, 0A), Rank__c=(A, B), Store__c=(01B1, 0AE0)}
public with sharing class JsonParseringClass {
/**Opportunity Prefix*/
private static final String SOBJECT_TYPE_PREFIX_OPPORTUNITY
= Schema.SObjectType.Opportunity.getKeyPrefix();
public JsonParseringClass() {
Set<String> hQSets = new Set<String>();
Set<String> officeSets = new Set<String>();
Set<String> storeSets = new Set<String>();
Set<String> rankSets = new Set<String>();
Project__c projectItem = [SELECT Id,HQ__c, Office__c, Store__c, Rank__c,SYS_ApprovalJson__c FROM Project__c WHERE Id = 'a025g00000AJaWUAA1'];
if (projectItem.SYS_ApprovalJson__c != null && projectItem.SYS_ApprovalJson__c.startsWith('[') && projectItem.SYS_ApprovalJson__c.endsWith(']')) {
List<Object> useAppListJson = (List<Object>)JSON.deserializeUntyped(projectItem.SYS_ApprovalJson__c);
if (useAppListJson != null) {
for (Object listItem : useAppListJson) {
if(listItem instanceof Map<String,Object>) {
Map<String,Object> mapItem = (Map<String,Object>)listItem;
System.debug('>>>mapItem>>>' + mapItem);
for (Object objItem : (List<Object>)mapItem?.get('HQ__c')) {
hQSets.add((String)objItem);
}
for (Object objItem : (List<Object>)mapItem?.get('Office__c')) {
officeSets.add((String)objItem);
}
for (Object objItem : (List<Object>)mapItem?.get('Store__c')) {
storeSets.add((String)objItem);
}
for (Object objItem : (List<Object>)mapItem?.get('Rank__c')) {
rankSets.add((String)objItem);
}
}
}
}
}
System.debug('>>>hQSets>>>' + hQSets);
System.debug('>>>officeSets>>>' + officeSets);
System.debug('>>>storeSets>>>' + storeSets);
System.debug('>>>rankSets>>>' + rankSets);
}
}
Log结果
public with sharing class JsonParseringClass {
/**Opportunity Prefix*/
private static final String SOBJECT_TYPE_PREFIX_OPPORTUNITY
= Schema.SObjectType.Opportunity.getKeyPrefix();
public JsonParseringClass() {
Set<String> hQSets = new Set<String>();
Set<String> officeSets = new Set<String>();
Set<String> storeSets = new Set<String>();
Set<String> rankSets = new Set<String>();
List<Object> useAppListJson = new List<Object>();
Project__c projectItem = [SELECT Id,HQ__c, Office__c, Store__c, Rank__c,SYS_ApprovalJson__c FROM Project__c WHERE Id = 'a025g00000AJaWUAA1'];
if (projectItem.SYS_ApprovalJson__c != null && projectItem.SYS_ApprovalJson__c.startsWith('[') && projectItem.SYS_ApprovalJson__c.endsWith(']')) {
useAppListJson = (List<Object>)JSON.deserializeUntyped(projectItem.SYS_ApprovalJson__c);
if (useAppListJson != null) {
for (Object listItem : useAppListJson) {
if(listItem instanceof Map<String,Object>) {
Map<String,Object> mapItem = (Map<String,Object>)listItem;
System.debug('>>>mapItem>>>' + mapItem);
for (Object objItem : (List<Object>)mapItem?.get('HQ__c')) {
hQSets.add((String)objItem);
}
for (Object objItem : (List<Object>)mapItem?.get('Office__c')) {
officeSets.add((String)objItem);
}
for (Object objItem : (List<Object>)mapItem?.get('Store__c')) {
storeSets.add((String)objItem);
}
for (Object objItem : (List<Object>)mapItem?.get('Rank__c')) {
rankSets.add((String)objItem);
}
}
}
}
}
if (projectItem?.HQ__c?.split(';') != null && projectItem?.HQ__c?.split(';').size() > 0) {
for (String item : projectItem?.HQ__c?.split(';')) {
hQSets.add(item);
}
}
if (projectItem?.Office__c?.split(';') != null && projectItem?.Office__c?.split(';').size() > 0) {
for (String item : projectItem?.Office__c?.split(';')) {
officeSets.add(item);
}
}
if (projectItem?.Store__c?.split(';') != null && projectItem?.Store__c?.split(';').size() > 0) {
for (String item : projectItem?.Store__c?.split(';')) {
storeSets.add(item);
}
}
if (projectItem?.Rank__c?.split(';') != null && projectItem?.Rank__c?.split(';').size() > 0) {
for (String item : projectItem?.Rank__c?.split(';')) {
rankSets.add(item);
}
}
System.debug('>>>hQSets>>>' + hQSets);
System.debug('>>>officeSets>>>' + officeSets);
System.debug('>>>storeSets>>>' + storeSets);
System.debug('>>>rankSets>>>' + rankSets);
List<Object> updateTempList = new List<Object>();
Map<String,Object> mapTempItem = new Map<String,Object>();
mapTempItem.put('Datetime', Datetime.now().format('yyyy-MM-dd kk:mm:ss.S', 'Asia/Tykyo'));
mapTempItem.put('HQ__c', projectItem?.HQ__c?.split(';'));
mapTempItem.put('Office__c', projectItem?.Office__c?.split(';'));
mapTempItem.put('Store__c', projectItem?.Store__c?.split(';'));
mapTempItem.put('Rank__c', projectItem?.Rank__c?.split(';'));
updateTempList.add(mapTempItem);
useAppListJson.addAll(updateTempList);
String serializeToJson = JSON.serialize(useAppListJson);
projectItem.SYS_ApprovalJson__c = serializeToJson;
update projectItem;
}
}
项目SYS_ApprovalJson__c 的更新结果
[{"Rank__c":["S"],"Store__c":["01B1"],"Office__c":["01"],"HQ__c":["01"],"Datetime":"2022-05-28T08:34:15.000Z"},{"Rank__c":["A","B"],"Store__c":["01B1","0AE0"],"Office__c":["01","0A"],"HQ__c":["01","0A"],"Datetime":"2022-08-31T12:17:52.000Z"},{"Rank__c":["A","C"],"Store__c":["0CH5"],"Office__c":["01","0A"],"HQ__c":["02","0B"],"Datetime":"2022-09-23 14:32:09.388"}]
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。