Vorrei formattare l'output json in modo che contenga solo una stringa per oggetti con un solo elemento e non matrice. Di seguito è il mio POJO.
@Component("myVO")
@Scope("prototype")
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(propOrder = {"a", "b", "c", "d", "e", "f", "sectionList"})
public class MyVO extends ServiceResponseTO {
private static final long serialVersionUID = -9190950749517871535L;
@XmlElement(name="ABC")
private String a;
@XmlElement(name="DEF")
private String b;
@XmlElement(name="GHI")
private String c;
@XmlElement(name="JKL")
private String d;
@XmlElement(name="MNO")
private String e;
@XmlElement(name="PQR")
private String f;
@XmlElement(name="section")
@XmlJavaTypeAdapter(MyAdapter.class)
private List<Map<String, String>> sectionList ;
//corresponding getters and setters
@Override
public String toString() {
return "DetailsVO [a=" + a + ", b=" + b
+ ", c=" + c + ", d=" + d
+ ", e=" + e + ", f=" + f
+ ", sectionList=" + sectionList+ "]";
}
}
Il problema riguarda il codice dell'adattatore riportato di seguito.
class MyAdapter extends XmlAdapter<KeyValuesAsNodeList, Map<String, String>> {
public KeyValuesAsNodeList marshal(Map<String,String> map) throws Exception {
KeyValuesAsNodeList mapType = new KeyValuesAsNodeList();
for(Map.Entry<String,String> entry : map.entrySet()){
if(entry.getKey().equalsIgnoreCase("key1")
mapType.key1 = entry.getValue().toString();
if(entry.getKey().equalsIgnoreCase("key2")
mapType.key2 = entry.getValue().toString();
if(entry.getKey().equalsIgnoreCase("key3")
mapType.key3 = entry.getValue().toString();
if(entry.getKey().equalsIgnoreCase("key3")
mapType.key3 = entry.getValue().toString();
if( entry.getValue().getClass().equals(HashMap.class) || entry.getValue().getClass().equals(LinkedHashMap.class)){
mapType.entries.add(new JAXBElement(new QName(entry.getKey(),String,class,marshal(entry.getValue()))));
}
else{
// For the issue I mentioned.. The value is a string.. so it enters this part. Here the problem lies
mapType.entries.add(new JAXBElement(new QName(entry.getKey(),String,class,entry.getValue())));
}
}
}
}
public Map<String, String> unmarshal(Map<String, String> ) throws Exception {
return null;
}
}
class KeyValuesAsNodeList
{
@XmlAttribute
public String key1 ;
@XmlAttribute
public Integer key2;
@XmlAttribute
public String key3;
@XmlAttribute
public String key4;
//since I have given JAXBElement as a list, even my strings are retured as an array in JSON.
List<JAXBElement> entries = new ArrayList<JAXBElement>();
}
Usando il mio adattatore sopra, se provo a convertire in JSON, la risposta è formata come sotto.
{"returnCode":"00","response":{"type":"myVO","ABC":"A","DEF":"B","GHI":"C","JKL":"D","MNO":"E","PQR":"F","section":[{"name":"SectionOne","configuration":"OOO","NAME":["N"],"SECTIONNAME":["A"]}]}}
La risposta sopra contiene array JSON anche per un singolo elemento. Voglio che l'output sopra sia formato come sotto.
La risposta di cui ho bisogno è:
{"returnCode":"00","response":{"type":"myVO","ABC":"A","DEF":"B","GHI":"C","JKL":"D","MNO":"E","PQR":"F","section":[{"name":"SectionOne","configuration":"OOO","NAME":"N","SECTIONNAME":"A"}]}}
Non voglio array per NAME e SECTIONNAME. Vorrei cambiare l'adattatore in modo tale da ottenere l'output JSON desiderato e anche che non influisca sul mio XML
P.S: Con questo adattatore, la mia risposta XML è perfetta. Qualsiasi aiuto è apprezzato. Grazie.
risposte:
0 per risposta № 1Cambia il tipo di dati del campo sectionList in:
private Map<String, String> sectionList ;
0 per risposta № 2
L'utilizzo di Eclipse Moxy ha risolto il problema. Il mio requisito era di avere le chiavi HashMap come nodi XML. L'uso di @XmlVariableNode invece di List ha fatto il tutto