8 回答
TA贡献1874条经验 获得超12个赞
可以用hashmap,放到list之后,再放到map中,通过Gson转化。
比如
Map<String,Stirng> nameMap = new HashMap<String,String>();
nameMap .put(name,"张三");
nameMap .put(remark,"售货员");
Map<String,Stirng> dataMap= new HashMap<String,String>();
List dataList =new ArrayList();
for(int i=0;i<=4;i++){
dataMap.put("data","数据1");
dataMap.put("type","666");
}
dataList.add(dataMap);
nameMap.put(display,dataList);
String s = new Gson().toJson(nameMap);
代码稍微调整一下应该能运行。
TA贡献1779条经验 获得超6个赞
public class JsonUtils{
private static final ObjectMapper mapper = new ObjectMapper();
public static String Object2Json(Object o)throws BusinessException{
StringWriter writer = new StringWriter();
try {
mapper.writeValue(writer, o);
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException(0, "Entity转换成Json时出现异常。", e);
}
return writer.toString();
}
添加回答
举报