如何将JSON解析为JTree,反之亦然

时间:2021-02-08 12:36:36

I'm using Java, and I want to parse a complex JSON String (one that contains objects, arrays with values, and arrays of objects to a JTree) and vice versa. I was able to create a method that parses the JSON string (using Jackson ObjectMapper and JsonNode) to a JTree, but now I want the tree to be editable. And once changed, I want to be able to parse it back to a JSON String or to my Java class represented by the JSON String. Is there a proper way to do this?

我正在使用Java,我想解析一个复杂的JSON字符串(包含对象,包含值的数组和对象数组到JTree),反之亦然。我能够创建一个方法,将JSON字符串(使用Jackson ObjectMapper和JsonNode)解析为JTree,但现在我希望树可以编辑。一旦改变,我希望能够将其解析回JSON字符串或JSON字符串表示的Java类。有没有正确的方法来做到这一点?

1 个解决方案

#1


0  

I can advice you to use GSON(https://sites.google.com/site/gson/gson-user-guide) to convert your data to complex JTree structure and vice versa. So you can grab your Java class represented by the JSON String from your tree and convert it to JSON easily with GSON.

我建议您使用GSON(https://sites.google.com/site/gson/gson-user-guide)将您的数据转换为复杂的JTree结构,反之亦然。因此,您可以从树中获取由JSON字符串表示的Java类,并使用GSON轻松将其转换为JSON。

As example:

ComplexTreeObject obj = new ComplexTreeObject();
Gson gson = new Gson();
String json = gson.toJson(obj); 

#1


0  

I can advice you to use GSON(https://sites.google.com/site/gson/gson-user-guide) to convert your data to complex JTree structure and vice versa. So you can grab your Java class represented by the JSON String from your tree and convert it to JSON easily with GSON.

我建议您使用GSON(https://sites.google.com/site/gson/gson-user-guide)将您的数据转换为复杂的JTree结构,反之亦然。因此,您可以从树中获取由JSON字符串表示的Java类,并使用GSON轻松将其转换为JSON。

As example:

ComplexTreeObject obj = new ComplexTreeObject();
Gson gson = new Gson();
String json = gson.toJson(obj);