在适配器代码中列出<jaxbelement>影响JSON格式

时间:2015-07-14 15:30:22

标签: json xml jaxb

我想将json输出格式化为仅包含只有一个元素而不是数组的对象的字符串。以下是我的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+ "]";
}


}

问题在于我的适配器代码如下。

    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>();
}

使用我的上述适配器,如果我尝试转换为JSON,则响应形成如下。

{"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"]}]}}

上述响应即使对于单个元素也包含JSON数组。我希望上面的输出形成如下。

我需要的回应是:

{"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"}]}}

我不想要NAME和SECTIONNAME的数组。我希望以这样的方式更改适配器,以便获得所需的JSON输出,这也不会影响我的XML

P.S:有了这个适配器,我的XML响应是完美的。 任何帮助表示赞赏。感谢。

2 个答案:

答案 0 :(得分:0)

将sectionList字段的数据类型更改为:

private Map<String, String> sectionList ;

答案 1 :(得分:0)

使用Eclipse Moxy解决了这个问题。我的要求是将HashMap键作为XML节点。使用@XmlVariableNode代替List完成了整个事情