Json的规格规定如下
1) 并列的数据之间用逗号(", ")分隔。
2) 映射用冒号(": ")表示。
3) 并列数据的集合(数组)用方括号("[ ]")表示。
4) 映射的集合(对象)用大括号("{ }")表示。
1.反序列化
有如下两条Json格式的数据,两条数据之间用逗号(", ")分隔
代码语言:javascript复制[["2022-05-31T12:17:52.000 0000","0065g00000FFUJtAAP","0065g00000FFUJtAAP"],["2022-06-07T04:57:32.000 0000","0065g000007pkZOAAY","0c86D0000008OUqQAM"]]
把它存储在Object的项目中,进行反序列化处理。
【deserializeUntyped】方法用于将JSON内容反序列化成基本数据类型的集合,不能反序列化sObject类型。
代码语言:javascript复制public with sharing class JsonParseringClass {
public JsonParseringClass() {
Project__c projectItem = [SELECT Id,SYS_Approval__c FROM Project__c WHERE Id = 'a025g00000AJaWUAA1'];
if (projectItem?.SYS_Approval__c != null
&& projectItem?.SYS_Approval__c.startsWith('[')
&& projectItem?.SYS_Approval__c.endsWith(']')) {
List<Object> deserializeUntypedJson = (List<Object>)JSON.deserializeUntyped(projectItem?.SYS_Approval__c);
System.debug('>deserializeUntypedJson>>' deserializeUntypedJson);
}
}
}
Log出力结果:
代码语言:javascript复制USER_DEBUG [12]|DEBUG|>deserializeUntypedJson>>((2022-05-31T12:17:52.000 0000, 0065g00000FFUJtAAP, 0065g00000FFUJtAAP), (2022-06-07T04:57:32.000 0000, 0065g000007pkZOAAY, 0c86D0000008OUqQAM))
详细出力
代码语言:javascript复制public with sharing class JsonParseringClass {
public JsonParseringClass() {
Project__c projectItem = [SELECT Id,SYS_Approval__c FROM Project__c WHERE Id = 'a025g00000AJaWUAA1'];
if (projectItem?.SYS_Approval__c != null
&& projectItem?.SYS_Approval__c.startsWith('[')
&& projectItem?.SYS_Approval__c.endsWith(']')) {
List<Object> deserializeUntypedJson = (List<Object>)JSON.deserializeUntyped(projectItem?.SYS_Approval__c);
for (Object obj : deserializeUntypedJson) {
System.debug('>obj>>' obj);
for (Object item : (List<Object>)obj) {
System.debug('>item>>' item);
}
}
}
}
}
2.序列化
serialize方法用于将基本数据类型序列化成Json格式,如下取出表中的Json数据,进行添加并序列化之后更新对应项目。
代码语言:javascript复制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_Approval__c FROM Project__c WHERE Id = 'a025g00000AJaWUAA1'];
if (projectItem?.SYS_Approval__c != null
&& projectItem?.SYS_Approval__c.startsWith('[')
&& projectItem?.SYS_Approval__c.endsWith(']')) {
List<Object> deserializeUntypedJson = (List<Object>)JSON.deserializeUntyped(projectItem?.SYS_Approval__c);
System.debug('>OldJson>>' deserializeUntypedJson);
List<Object> jsonNewList = new List<Object>();
List<Object> jsonObNewList = new List<Object>();
jsonObNewList.add(Datetime.now().format('yyyy-MM-dd kk:mm:ss.S', 'Asia/Tykyo'));
jsonObNewList.add('0065g000007pkVyAAI');
jsonObNewList.add('0065g000007pkWFAAY');
jsonNewList.add(jsonObNewList);
deserializeUntypedJson.addAll(jsonNewList);
System.debug('>NewJson>>' deserializeUntypedJson);
for (Object obj : deserializeUntypedJson) {
for (Object item : (List<Object>)obj) {
if (((String)item).startsWith(SOBJECT_TYPE_PREFIX_OPPORTUNITY)) {
//System.debug('>Opportunity>>' (String)item);
}
}
}
String serializeToJson = JSON.serialize(deserializeUntypedJson);
projectItem.SYS_Approval__c = serializeToJson;
update projectItem;
System.debug('>update>>' projectItem.SYS_Approval__c);
}
}
}
代码语言:javascript复制USER_DEBUG [12]|DEBUG|>OldJson>>((2022-05-31T12:17:52.000 0000, 0065g00000FFUJtAAP, 0065g00000FFUJtAAP), (2022-06-07T04:57:32.000 0000, 0065g000007pkZOAAY, 0c86D0000008OUqQAM))
代码语言:javascript复制USER_DEBUG [22]|DEBUG|>NewJson>>((2022-05-31T12:17:52.000 0000, 0065g00000FFUJtAAP, 0065g00000FFUJtAAP), (2022-06-07T04:57:32.000 0000, 0065g000007pkZOAAY, 0c86D0000008OUqQAM), (2022-09-23 06:50:14.163, 0065g000007pkVyAAI, 0065g000007pkWFAAY))
代码语言:javascript复制USER_DEBUG [32]|DEBUG|>update>>[["2022-05-31T12:17:52.000 0000","0065g00000FFUJtAAP","0065g00000FFUJtAAP"],["2022-06-07T04:57:32.000 0000","0065g000007pkZOAAY","0c86D0000008OUqQAM"],["2022-09-23 06:50:14.163","0065g000007pkVyAAI","0065g000007pkWFAAY"]]