简体   繁体   English

转换地图 <Enum, Object> 到JSON

[英]Converting Map<Enum, Object> to JSON

I build a json on server like that: 我在服务器上构建一个json:

List<Object> resp = new ArrayList<>();
for (String str : buffer.toString().split("\\n")) {
    for (String data : str.split("\\s")) {
        if (data.matches("[\\w|\\W]+") && !data.isEmpty()) {
            Child.set(Child.children, "null");
            Child.set(Child.parent, chain.name());
            Child.set(Child.name, data);
        }
        Child.setChild(Child.get());
    }
}

Child.set(Child.children, Child.getChild().toArray());
Child.set(Child.parent, "null");
Child.set(Child.name, chain.name());

The problem is that Child is HashMap<Child, Object> but when I put Child.set(Child.children, Child.getChild().toArray()); 问题是Child是HashMap<Child, Object>但是当我把Child.set(Child.children, Child.getChild().toArray()); to Map and then convert the Map object with Google Gson to Json it breaks with error: 要映射,然后将Google Gson的Map对象转换为Json,它会中断错误:

oejw.WebSocketConnectionRFC6455:java.lang.StackOverflowError

The problem is not with WebSocket I think, because when I'm previous convert to json the Child.getChild().toArray() and then put it to the map, there are no any errors, but the generated json is not fully corrected. 我认为问题不在于WebSocket,因为当我以前转换为json的Child.getChild().toArray()然后将它放到地图上时,没有任何错误,但生成的json没有完全纠正。

The problem was with converting to Json. 问题在于转换为Json。 Gson converts with stack overflow error. Gson转换为堆栈溢出错误。 Solves with using JSONObject and JSONArray. 使用JSONObject和JSONArray解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM