RestContext.response.responseBody = Blob.valueOf(JSON.serialize(locations));
JSON can handle almost any data type, including maps that include serializable types. The exceptions to this rule are items that can't be serialized, like SavePoint, SObjectField and so on.
Answer from sfdcfox on Stack ExchangeRestContext.response.responseBody = Blob.valueOf(JSON.serialize(locations));
JSON can handle almost any data type, including maps that include serializable types. The exceptions to this rule are items that can't be serialized, like SavePoint, SObjectField and so on.
If you really want to convert a Map to String you can do it by using String.valueOf()
Map<String, Integer> mapVal = new Map<String, Integer> {'zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3};
String mapString = String.valueOf(mapVal);
I suspect what you want to do is really convert to JSON, which in that case you would do JSON.serialize as sfdcfox suggested.
apex - How to convert Map<String,Object> into Map<string,string>()? - Salesforce Stack Exchange
Apex Map<String,Object> need to get object values - Salesforce Stack Exchange
Feed Item Detail | Salesforce Trailblazer Community
visualforce - Convert JSON string into apex map - Salesforce Stack Exchange
So you can try like this
map<string,string> newMap= new Map<string,string>();
for(String strKey: mapValues.keyset())
{
newMap.put(strKey, String.valueof(mapValues.get(strKey)));
}
I have tried this worked for me:
enter code here
map<string,object> mspstrToObj = new Map<string,object>();
mspstrToObj.put('key1', 'obj1');
map<string,string> mspstrTostr = new Map<string,string>();
mspstrTostr.put('strKey1',string.valueof(mspstrToObj.get('key1')));
system.debug('=======mspstrTostr====='+mspstrTostr);
You need to create new Map and put the values as per above.
You are really working uphill by doing this all untyped, but you can get the value you want this way:
list<Object> l = (list<Object>) meta.get('addressDetails');
map<String,Object> m = (map<String,Object>) l[0];
Integer i = (Integer) m.get('addressId');
Honestly, if you make use of the Apex type system, you will have an easier time working with the data. If you create a class for the incoming data structure, abbreviated as:
public class Meta{
public list<addressDetail> addressDetails {get; set;}
public class addressDetail{
public Integer addressId {get; set;}
}
}
you will be able to access the data from your JSON strings as so:
Meta MyMeta = (Meta) JSON.deserialize(viewPersonValue,Meta.class);
system.debug(MyMeta.addressDetails[0].addressId);
May be this simple example can help
string viewPersonValue = '{"name":"John",
"age":30,
"cars":
{"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"}
}';
Map<String, Object> meta = (Map<String, Object> JSON.deserializeUntyped(viewPersonValue);
Map<String, Object> carMap = (Map<String, Object>) meta.get('cars');
system.debug(carMap.get('car1'));
You cant convert the map to a list without an id. Alternatively why to convert it to List. Use the map itself. Below is a code example of how you can use it using your JSON response. For example, I want to insert this JSON response into the contact record, so I can map it using the MAP itself.:
@RestResource(urlMapping='/URIId/*') global class OwneriCRM {
@HttpPut
global static String UpdateOwnerinfo(){
RestRequest req= RestContext.request;
RestResponse res= RestContext.response;
res.addHeader('Content-type','application/JSON');
Map<String,Object> responseresult=new Map<String,Object>();
Map<String,Object> results= (Map<String,Object>)JSON.deserializeUntyped(req.requestBody.toString());
List<contact> insertList = new List<contact>();
for (String key : results.keySet()){
System.debug(results.get(key));
contact c= new contact();
c.ContactField__C = results.get(id);
c.ContactField1__C = results.get(city);
c.ContactField2__C = results.get(conference);
c.ContactField3__C = results.get(division);
c.ContactField3__C = results.get(full_name);
c.ContactField3__C = results.get(name);
insertList.add(c);
}
if(!insertList.Isempty()){
update insertList;
}
Question can be disregarded. Not sure if there is a way to withdraw the question.
Question was based on the fact that previous APIs had been sent in the following format:
{"data": {"more stuff": "stuff"} }
And the map key was 'data' with a list. In this scenario, the whole API was a list and the keys to the map were set in place with the actual values instead.