如何根据反序列化Untyped响应获取键值值
How to get key value value based on deserializeUntyped response
这是我的JSONbody
我需要的键值
例如:if tenantcode = 200 我需要的是“2”
string str = '{"10":{"name":"IP Auth","tenantcode":"204"},"11":{"name":"Registration","tenantcode":"205"},"13":{"name":"API Demo","tenantcode":"206"},"2":{"name":" Test","tenantcode":"200"},"29":{"name":"testing 2","tenantcode":"207"},"3":{"name":"Training ","tenantcode":"201"},"30":{"name":" Test222","tenantcode":"208"},"31":{"name":"Martin","tenantcode":"212"},"32":{"name":"API Demo Customer","tenantcode":"209"},"4":{"name":"Adrian ","tenantcode":"202"},"9":{"name":"Stephen","tenantcode":"203"}}';
Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(str);
for(object obj : resultsMap.values()){
Map<String,Object> data = (Map<String,Object>)obj;
system.debug('utlevel30description---'+(String)data.get('tenantcode'));
}
问题是您正在遍历映射的值并且您无法再访问键。
您可以尝试以下操作以同时访问键和值。
string str = '{"10":{"name":"IP Auth","tenantcode":"204"},"11":{"name":"Registration","tenantcode":"205"},"13":{"name":"API Demo","tenantcode":"206"},"2":{"name":" Test","tenantcode":"200"},"29":{"name":"testing 2","tenantcode":"207"},"3":{"name":"Training ","tenantcode":"201"},"30":{"name":" Test222","tenantcode":"208"},"31":{"name":"Martin","tenantcode":"212"},"32":{"name":"API Demo Customer","tenantcode":"209"},"4":{"name":"Adrian ","tenantcode":"202"},"9":{"name":"Stephen","tenantcode":"203"}}';
Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(str);
for(String key : resultsMap.keySet()){
Map<String,Object> data = (Map<String,Object>)resultsMap.get(key);
system.debug('utlevel30description---'+(String)data.get('tenantcode'));
if((String)data.get('tenantcode') == '200'){
system.debug('your key is ' + key);
}
}
这是我的JSONbody
我需要的键值
例如:if tenantcode = 200 我需要的是“2”
string str = '{"10":{"name":"IP Auth","tenantcode":"204"},"11":{"name":"Registration","tenantcode":"205"},"13":{"name":"API Demo","tenantcode":"206"},"2":{"name":" Test","tenantcode":"200"},"29":{"name":"testing 2","tenantcode":"207"},"3":{"name":"Training ","tenantcode":"201"},"30":{"name":" Test222","tenantcode":"208"},"31":{"name":"Martin","tenantcode":"212"},"32":{"name":"API Demo Customer","tenantcode":"209"},"4":{"name":"Adrian ","tenantcode":"202"},"9":{"name":"Stephen","tenantcode":"203"}}';
Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(str);
for(object obj : resultsMap.values()){
Map<String,Object> data = (Map<String,Object>)obj;
system.debug('utlevel30description---'+(String)data.get('tenantcode'));
}
问题是您正在遍历映射的值并且您无法再访问键。
您可以尝试以下操作以同时访问键和值。
string str = '{"10":{"name":"IP Auth","tenantcode":"204"},"11":{"name":"Registration","tenantcode":"205"},"13":{"name":"API Demo","tenantcode":"206"},"2":{"name":" Test","tenantcode":"200"},"29":{"name":"testing 2","tenantcode":"207"},"3":{"name":"Training ","tenantcode":"201"},"30":{"name":" Test222","tenantcode":"208"},"31":{"name":"Martin","tenantcode":"212"},"32":{"name":"API Demo Customer","tenantcode":"209"},"4":{"name":"Adrian ","tenantcode":"202"},"9":{"name":"Stephen","tenantcode":"203"}}';
Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(str);
for(String key : resultsMap.keySet()){
Map<String,Object> data = (Map<String,Object>)resultsMap.get(key);
system.debug('utlevel30description---'+(String)data.get('tenantcode'));
if((String)data.get('tenantcode') == '200'){
system.debug('your key is ' + key);
}
}