在Java中订购新的Gson().toJson()

在Java中订购新的Gson().toJson(),java,android,json,string,gson,Java,Android,Json,String,Gson,此代码使用带有两个列表的映射 在Map中,乍一看,这似乎是预期的输出。由于字符串类中的hashCode实现,ttDet位于ttPrm之前,因为它的hashCode较小 使用LinkedHashMap可能会有所帮助。试一试 String parametroGSON =""; try { Map<String, List<?>> mapa = new LinkedHashMap<String, List<?>>(); mapa.put("ttP

此代码使用带有两个
列表的映射

Map中,乍一看,这似乎是预期的输出。由于字符串类中的hashCode实现,ttDet位于ttPrm之前,因为它的hashCode较小

使用LinkedHashMap可能会有所帮助。试一试

String parametroGSON ="";
try {
  Map<String, List<?>> mapa = new LinkedHashMap<String, List<?>>();
  mapa.put("ttPrm", listParam);
  mapa.put("ttDet", ListaArt);
  parametroGSON = new Gson().toJson(mapa);
} catch (Exception e) { }
System.out.println(json);
String parametroGSON=”“;
试一试{
地图>();
mapa.put(“ttPrm”,listpram);
mapa.put(“ttDet”,ListaArt);
parametroGSON=new Gson().toJson(mapa);
}捕获(例外e){}
System.out.println(json);
相反


根据具体要求,另一个映射可能有用,LinkedHashMap保留插入顺序(与LinkedHashSet相同)。

您的问题有当前和预期输出一个,在更详细的描述中,当前和预期输出是正确的。-固定问题。
 {"ttDet":[{"dsArticulo":"PEPSI 500 X 12 PET"}],"ttPrm":[{"accion":"upd","cod_depo":"1"}]}
{"ttPrm":[{"accion":"upd","cod_depo":"1"}] ,"ttDet":[{"dsArticulo":"PEPSI 500 X 12 PET"}]}
String parametroGSON ="";
try {
  Map<String, List<?>> mapa = new LinkedHashMap<String, List<?>>();
  mapa.put("ttPrm", listParam);
  mapa.put("ttDet", ListaArt);
  parametroGSON = new Gson().toJson(mapa);
} catch (Exception e) { }
System.out.println(json);