在java中使用Map序列化Json - 更改输出

时间:2018-06-03 05:48:43

标签: java arrays json dictionary

我有这个返回json的方法。

  public Map<String,Object> findAllById(Integer id,Integer channelId){


 Object[] electronicBill=null;


try{    
electronicBill =(Object[])ElectronicBillRepository.findAllById(id);
electronicBill=(Object[]) electronicBill[0];
String clave = null;
Date fecha = null ; 
BigInteger numIdentificacion ;
Integer tipoidentificacion ; 

clave=(String) electronicBill[0];
fecha=(Date) electronicBill[1];
numIdentificacion= (BigInteger) electronicBill[2];
tipoidentificacion=(Integer) electronicBill[3];



 proofXML.put("clave", clave);
 proofXML.put("fecha", fecha);
 proofXML.put("tipoIdentificacion", numIdentificacion);
 proofXML.put("numeroIdentificacion", tipoidentificacion);

}catch(Exception e){
    throw new RuntimeException();
}
return proofXML;
}

上一个方法生成此响应

    {
"clave": "50618101700040226022200100001010000405353010803003",
"fecha": 201601010000000600,
 "tipoIdentificacion": 402260222,
 "numeroIdentificacion": 2
}

现在我的查询是相关的,我希望以下列方式显示输出

  {
    "clave": "50601011600310112345600100010100000000011999999999",
    "fecha": "2016-01-01T00:00:00-0600",
    "emisor": {
    "tipoIdentificacion": "02",
     "numeroIdentificacion": "402260222"
  },

   "comprobanteXml": " "
  }

任何与此有关的帮助,在此先感谢

1 个答案:

答案 0 :(得分:0)

你可以这样做:

  Map<String, Object> main = new LinkedHashMap<>();
    Map<String, Object> sub = new LinkedHashMap<>();
    sub.put("tipoIdentificacion", "02");
    sub.put("numeroIdentificacion", "402260222");

    main.put("clave", "50601011600310112345600100010100000000011999999999");
    main.put("fecha", "2016-01-01T00:00:00-0600");
    main.put("emisor" , sub);
    main.put("comprobanteXml", "test");

但请看看这里:How to fluently build JSON in Java?