Serialize a Map into a specific JSON object
Here how you can achieve it.
map<string, object> mapToSerialize = new map<string, object>();
mapToSerialize.put('type', 'TEST');
//add you subtype like this
mapToSerialize.put('subtypes', new list<string>{'TEST'});
string jsonstring = JSON.serialize(mapToSerialize);
May be the following sample code helps:
Map<Object,List<Object>> mapTypeSubType= new Map<Object,List<Object>>();
for(Integer i=0; i<3; i++){
if(!mapTypeSubType.containskey('"type":"TEST"')){
mapTypeSubType.put('"type":"TEST"',new List<Object> {'"subtypes":"TEST"'});
}else{
mapTypeSubType.get('"type":"TEST"').add('TEST');
}
}
System.debug(mapTypeSubType);
String jsonMap=JSON.serialize(mapTypeSubType);
System.debug(jsonMap);
//Here's the debug response:
{"\"type\":\"TEST\"":["\"subtypes\":\"TEST\"","TEST","TEST"]}