如何使用 Apex 从嵌套的 JSON 中读取值?
How to read values from nested JSON using Apex?
我正在将 JSON 字符串从 Javascript 传递到 Apex。我想访问的字段是每个“字段”实例的“名称”和“Content_Copy__c”,但我似乎无法弄清楚。我的一部分认为我需要创建地图的地图,但我也不确定该怎么做。在 try-catch 中,try 不起作用,我认为这是问题的一部分,因为它进入了 catch。
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy__c":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy__c":"ipsum"}}]';
Object o = JSON.deserializeUntyped(jsonListString);
try{
Map<String, Object> m = (Map<String, Object>) o;
System.debug('The Map is: ' + m);
}
catch(TypeException e){
List<Object> a = (List<Object>) o;
for(Object obj : a){
Map<String, Object> mapObject = (Map<String, Object>) obj;
Object s = mapObject.get('fields');
System.debug(s);
}
}
上面的代码在打印到调试器时给出了以下结果。
编辑:根据@Moustafa Ishak 的建议,我将周围的事情改成了这样:
public class fields{
public String Name {get; set;}
public String Content_Copy {get; set;}
public fields(String Name, String Content_Copy){
this.Name = Name;
this.Content_Copy = Content_Copy;
}
}
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy":"ipsum"}}]';
List<fields> response = (List<fields>)System.JSON.deserialize(jsonListString, List<fields>.class);
for(fields f : response){
System.debug('Name: ' + f.Name);
System.debug('Content_Copy: ' + f.Content_Copy);
}
这给了我空值,如下所示。我不得不将“Content_Copy__c”更改为“Content_Copy”,因为 Apex 对以“__c”结尾的非自定义对象存在问题。一旦我获得了字段的值,那将不是问题。如果我添加或删除另一个“字段”条目,上面的代码会识别,但不会识别“名称”和“Content_Copy”的值。
你能尝试创建响应包装器如下吗
public class response{
public String Name {get; set;}
public String Content_Copy {get; set;}
public response(String Name, String Content_Copy){
this.Name = Name;
this.Content_Copy = Content_Copy;
}
public class responseWrapper{
public response fields {get; set;}
}
}
然后您可以使用标准 JSON.deserialize 获取响应包装器(字段)列表
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy":"ipsum"}}]';
List<response.responseWrapper> response = (List<response.responseWrapper>)System.JSON.deserialize(jsonListString,
List<response.responseWrapper>.class);
system.debug('response ' +response);
for(response.responseWrapper f : response){
System.debug('fileds: ' + f.fields);
System.debug('name: ' + f.fields.name);
System.debug('Content_Copy: ' + f.fields.Content_Copy);
}
我正在将 JSON 字符串从 Javascript 传递到 Apex。我想访问的字段是每个“字段”实例的“名称”和“Content_Copy__c”,但我似乎无法弄清楚。我的一部分认为我需要创建地图的地图,但我也不确定该怎么做。在 try-catch 中,try 不起作用,我认为这是问题的一部分,因为它进入了 catch。
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy__c":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy__c":"ipsum"}}]';
Object o = JSON.deserializeUntyped(jsonListString);
try{
Map<String, Object> m = (Map<String, Object>) o;
System.debug('The Map is: ' + m);
}
catch(TypeException e){
List<Object> a = (List<Object>) o;
for(Object obj : a){
Map<String, Object> mapObject = (Map<String, Object>) obj;
Object s = mapObject.get('fields');
System.debug(s);
}
}
上面的代码在打印到调试器时给出了以下结果。
编辑:根据@Moustafa Ishak 的建议,我将周围的事情改成了这样:
public class fields{
public String Name {get; set;}
public String Content_Copy {get; set;}
public fields(String Name, String Content_Copy){
this.Name = Name;
this.Content_Copy = Content_Copy;
}
}
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy":"ipsum"}}]';
List<fields> response = (List<fields>)System.JSON.deserialize(jsonListString, List<fields>.class);
for(fields f : response){
System.debug('Name: ' + f.Name);
System.debug('Content_Copy: ' + f.Content_Copy);
}
这给了我空值,如下所示。我不得不将“Content_Copy__c”更改为“Content_Copy”,因为 Apex 对以“__c”结尾的非自定义对象存在问题。一旦我获得了字段的值,那将不是问题。如果我添加或删除另一个“字段”条目,上面的代码会识别,但不会识别“名称”和“Content_Copy”的值。
你能尝试创建响应包装器如下吗
public class response{
public String Name {get; set;}
public String Content_Copy {get; set;}
public response(String Name, String Content_Copy){
this.Name = Name;
this.Content_Copy = Content_Copy;
}
public class responseWrapper{
public response fields {get; set;}
}
}
然后您可以使用标准 JSON.deserialize 获取响应包装器(字段)列表
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy":"ipsum"}}]';
List<response.responseWrapper> response = (List<response.responseWrapper>)System.JSON.deserialize(jsonListString,
List<response.responseWrapper>.class);
system.debug('response ' +response);
for(response.responseWrapper f : response){
System.debug('fileds: ' + f.fields);
System.debug('name: ' + f.fields.name);
System.debug('Content_Copy: ' + f.fields.Content_Copy);
}