Java 如何将JSON数据保存到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9798021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save the JSON data to the database
提问by Judy
I am trying to save the JSON response of a website to the database. Latter I am accessing this JSON string for processing the data. But I am not able to parse it as JSON object. On analyzing I realize that the characters of strings needs to be escaped. Since I am saving the data as a string the data results which am getting back as a JSON data are not escaped in the database. Is there a way out how I can save the JSON data to the database.
我正在尝试将网站的 JSON 响应保存到数据库中。后来我访问这个 JSON 字符串来处理数据。但我无法将其解析为 JSON 对象。在分析时,我意识到字符串的字符需要转义。由于我将数据保存为字符串,因此作为 JSON 数据返回的数据结果不会在数据库中转义。有没有办法将 JSON 数据保存到数据库中。
Example:
例子:
{"RULE":[{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src="[^"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}},{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src="[^"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}}]}
Want to save in database as (also getting the response as) i.e. the " and \ are escaped
想要在数据库中保存为(也得到响应为)即“和\被转义
{"RULE":[{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src=\"[^\"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}},{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src=\"[^\"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}}]}
Here is the code I have used to save the data in the database
这是我用来将数据保存在数据库中的代码
// here the raw_data is the data from the website
JSONObject jo = new JSONObject(raw_data);
// Get the JSONObject value associated with the search result key.
jo = jo.getJSONObject("pipe");
jo = jo.getJSONObject("definition");
String def=jo.toString();
JSONArray jo1=jo.getJSONArray("modules");
JSONArray jo2=jo.getJSONArray("wires");
/*
* Write the contents in the data base
*
*
*/
def =def.replaceAll( "[']", "\\\'" ); //creates problem for strings with '
def =def.replaceAll( "&", "%26" );
String tablename="PipesTable2";
System.out.println(def);
database d=new database();
采纳答案by Erica
Have you considered using a library like Hymanson ( http://wiki.fasterxml.com/HymansonHome) to translate between Json objects and Java objects? Then you can use standard Java persistence tools and libraries to save it out to/read it in from your database, and it will handle all your escaping automatically.
您是否考虑过使用 Hymanson ( http://wiki.fasterxml.com/HymansonHome) 之类的库在 Json 对象和 Java 对象之间进行转换?然后您可以使用标准的 Java 持久性工具和库将其保存到您的数据库中/从您的数据库中读取它,它会自动处理您的所有转义。
Hymanson has two main ways that it translates between Json and Java objects. If you're planning on using direct JDBC calls for your persistence and you don't want to do much other processing, then you can get away with "Simple Data Binding". This just translates between Json and Java's built-in types (List, Map, String, Boolean, Number).
Hymanson 有两种主要方式在 Json 和 Java 对象之间进行转换。如果您打算使用直接 JDBC 调用来实现持久性,并且不想进行太多其他处理,那么您可以使用“简单数据绑定”。这只是在 Json 和 Java 的内置类型(List、Map、String、Boolean、Number)之间进行转换。
If you're planning on doing Java processing on the data, or you want to use a persistence framework like Hibernate, you'll need "Full Data Binding". This uses POJOs and annotations to provide a more complex structure to your objects.
如果您计划对数据进行 Java 处理,或者您想使用像 Hibernate 这样的持久性框架,您将需要“完整数据绑定”。这使用 POJO 和注释为您的对象提供更复杂的结构。
There is a good, very concise tutorial covering both options here: http://wiki.fasterxml.com/HymansonInFiveMinutes
这里有一个很好的,非常简洁的教程,涵盖了这两个选项:http: //wiki.fasterxml.com/HymansonInFiveMinutes
回答by kundan bora
I suggest you to create an class having properties mapped to json. You can convert object to json or json to object using third party tool.you can use Gson.
我建议您创建一个具有映射到 json 的属性的类。您可以使用第三方工具将对象转换为 json 或将 json 转换为对象。您可以使用 Gson。
Now what you can do when you get the josn you can convert this json to object and save that object's properties to database. when you retrieve the value, set these value to object's properties and then convert this object to json.
现在,当您获得 josn 时,您可以将这个 json 转换为对象并将该对象的属性保存到数据库中。检索值时,将这些值设置为对象的属性,然后将此对象转换为 json。
You can find Gson here - http://code.google.com/p/google-gson/
你可以在这里找到 Gson - http://code.google.com/p/google-gson/
This is easy to use.
这很容易使用。