将JSON字符串转换为JSON

时间:2018-01-19 10:42:24

标签: json gson spring-data-jpa

我正在使用Spring数据JPA查询Postgres DB以检索JSONB格式数据,该数据存储在DB中:

JTable table = new JTable(new MyTableModel()) {
    @Override
    public boolean editCellAt(int row, int column, EventObject e){
        boolean result = super.editCellAt(row, column, e);
        if( result ) {
            Component comp = getEditorComponent();
            if( comp instanceof JComboBox ) {
                final JComboBox combo = ( JComboBox ) comp;
                SwingUtilities.invokeLater( new Runnable() {

                    @Override
                    public void run() {
                        combo.showPopup();
                        // TODO Auto-generated method stub

                    }
                });
            }
        }
        return result;
    }
};  

但是我用双引号得到了回复,是否有一种方法可以使用spring数据JPA获取双引号或使用双引号转换回JSON?

我尝试了新的GSON()。tJson()和ObjectMapper,但没有运气任何帮助表示赞赏。

谢谢

2 个答案:

答案 0 :(得分:1)

试试这段代码!

您可以简单地使用parse函数将字符串解析为son对象。

    <!DOCTYPE html>
    <html>
    <body>

    <h2>Create Object from JSON String</h2>

    <p id="demo"></p>

    <script>
    var stringTojson='{ "name":"abc", "place":"xyz"}';
    var obj = JSON.parse(stringTojson);


    document.getElementById("demo").innerHTML = obj.name + ", " + obj.place;

    </script>

    </body>
    </html>

答案 1 :(得分:0)

您的 JSON 对象格式非常好,您应该没有问题反序列化将其重新发送回 POJO

假设您的 POJO 类名为public class SerializationTest { private static final String OBJECT = "{\n" + "\"name\":\"abc\",\n"+ "\"place\"=\"xyz\"\n"+ "}"; public static void main(String[] args) { System.out.println("Serialized object:\n" + OBJECT); Gson gson = new Gson(); Template template = gson.fromJson(OBJECT, Template.class); System.out.println(template); } private static class Template { private String name; private String place; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } @Override public String toString() { return "Template{" + "name='" + name + '\'' + ", place='" + place + '\'' + '}'; } } } ,此处为反序列化实现示例:

{{1}}