将字符串数组反序列化为CSV。

huangapple go评论113阅读模式
英文:

Deserialize array of strings to csv

问题

  1. 我有一个带有 JSON 负载的数据:
  2. "host_names": [
  3. "www.host1.com",
  4. "www.host2.com"
  5. ]
  6. 如何使用 Jackson 将其反序列化为 CSV 格式,例如:
  7. "www.host1.com,www.host2.com"
  8. 之前我将其反序列化为 `String[]`,但无法与 Hibernate 一起使用,所以现在我想将其反序列化为 CSV 格式。
  9. 编辑:
  10. 我想使用注解将 JSON 数组转换为一个字符串,其中数组的每个元素由逗号分隔。可能类似于 `@JsonRawValue` 这样的方式。目标是将该值通过 Hibernate 持久化到数据中。
英文:

I have a json payload with:

  1. "host_names": [
  2. "www.host1.com",
  3. "www.host2.com"
  4. ]

How can I deserialize this as a csv using Jackson - e.g.:

  1. "www.host1.com,www.host2.com"

Previously I was deserializing this as a String[] but I can't persist that with hibernate, so now I'm trying to deserialize it as a csv.

EDIT:

I want to use annotations to turn the json array into a string, where each element of the array is separated by a comma. Perhaps something like @JsonRawValue. The goal is to then persist the value to a data via hibernate.

答案1

得分: 1

  1. public static void main(String[] args) {
  2. String jsonStr = "{\"host_names\": [\r\n" +
  3. " \"www.host1.com\",\r\n" +
  4. " \"www.host2.com\"\r\n" +
  5. "]}";
  6. JSONObject jsonObject = new JSONObject(jsonStr);
  7. JSONArray hostNames = jsonObject.getJSONArray("host_names");
  8. String result = "";
  9. for(int i=0; i<hostNames.length(); i++) {
  10. if(!result.isEmpty())
  11. result = result + "," + hostNames.getString(i);
  12. else
  13. result = "\"" + hostNames.getString(i) + "\"";
  14. }
  15. System.out.println(result);
  16. }

Other approach based on annotation

Create a class

  1. class Server{
  2. @JsonProperty(value = "host_names")
  3. private List<String> hostNames;
  4. public List<String> getHostNames() {
  5. return hostNames;
  6. }
  7. public void setHostNames(List<String> hostNames) {
  8. this.hostNames = hostNames;
  9. }
  10. }

Now use com.fasterxml.jackson.databind.ObjectMapper to parse the JSON into this class

  1. public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
  2. String jsonStr = "{\"host_names\": [\r\n" +
  3. " \"www.host1.com\",\r\n" +
  4. " \"www.host2.com\"\r\n" +
  5. "]}";
  6. ObjectMapper mapper = new ObjectMapper();
  7. Server server = mapper.readValue(jsonStr, Server.class);
  8. System.out.println(server.getHostNames());
  9. }

Output

  1. [www.host1.com, www.host2.com]
英文:
  1. public static void main(String[] args) {
  2. String jsonStr = &quot;{\&quot;host_names\&quot;: [\r\n&quot; +
  3. &quot; \&quot;www.host1.com\&quot;,\r\n&quot; +
  4. &quot; \&quot;www.host2.com\&quot;\r\n&quot; +
  5. &quot;]}&quot;;
  6. JSONObject jsonObject = new JSONObject(jsonStr);
  7. JSONArray hostNames = jsonObject.getJSONArray(&quot;host_names&quot;);
  8. String result = &quot;&quot;;
  9. for(int i=0;i&lt;hostNames.length(); i++) {
  10. if(!result.isEmpty())
  11. result = result+&quot;,\&quot;&quot;+hostNames.getString(i)+&quot;\&quot;&quot;;
  12. else
  13. result = &quot;\&quot;&quot;+hostNames.getString(i)+&quot;\&quot;&quot;;
  14. }
  15. System.out.println(result);
  16. }

result

  1. &quot;www.host1.com&quot;,&quot;www.host2.com&quot;

Other approach based on annotation

Create a class

  1. class Server{
  2. @JsonProperty(value = &quot;host_names&quot;)
  3. private List&lt;String&gt; hostNames;
  4. public List&lt;String&gt; getHostNames() {
  5. return hostNames;
  6. }
  7. public void setHostNames(List&lt;String&gt; hostNames) {
  8. this.hostNames = hostNames;
  9. }
  10. }

Now use com.fasterxml.jackson.databind.ObjectMapper to parse the JSON into this class

  1. public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
  2. String jsonStr = &quot;{\&quot;host_names\&quot;: [\r\n&quot; +
  3. &quot; \&quot;www.host1.com\&quot;,\r\n&quot; +
  4. &quot; \&quot;www.host2.com\&quot;\r\n&quot; +
  5. &quot;]}&quot;;
  6. ObjectMapper mapper = new ObjectMapper();
  7. Server server = mapper.readValue(jsonStr, Server.class);
  8. System.out.println(server.getHostNames());
  9. }

output

  1. [www.host1.com, www.host2.com]

答案2

得分: 0

你可以稍微修改host_names属性的setter / getter,如下所示:

Bean类

  1. public class Server {
  2. @JsonProperty("host_names")
  3. private List<String> hostNames;
  4. @JsonGetter("host_names")
  5. public String getHostNames() {
  6. return String.join(",", hostNames);
  7. }
  8. @JsonSetter("host_names")
  9. public void setHostNames(List<String> hostNames) {
  10. this.hostNames = hostNames;
  11. }
  12. }

主方法

  1. public static void main(String[] args) throws JsonProcessingException {
  2. ObjectMapper mapper = new ObjectMapper();
  3. Server server = mapper.readValue("{\"host_names\":[\"www.host1.com\",\"www.host2.com\"]}", Server.class);
  4. String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(server);
  5. System.out.println(json);
  6. }

输出

  1. {
  2. "host_names" : "www.host1.com,www.host2.com"
  3. }

这样你可以在序列化时将其作为逗号分隔的字符串,而在反序列化时将其作为字符串数组。

英文:

You can slightly modify your setter / getter for host_names property as

Bean Class

  1. public class Server {
  2. @JsonProperty(&quot;host_names&quot;)
  3. private List&lt;String&gt; hostNames;
  4. @JsonGetter(&quot;host_names&quot;)
  5. public String getHostNames() {
  6. return String.join(&quot;,&quot;, hostNames);
  7. }
  8. @JsonSetter(&quot;host_names&quot;)
  9. public void setHostNames(List&lt;String&gt; hostNames) {
  10. this.hostNames = hostNames;
  11. }
  12. }

Main Method

  1. public static void main(String[] args) throws JsonProcessingException {
  2. ObjectMapper mapper = new ObjectMapper();
  3. Server server = mapper.readValue(&quot;{\&quot;host_names\&quot;:[\&quot;www.host1.com\&quot;,\&quot;www.host2.com\&quot;]}&quot;, Server.class);
  4. String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(server);
  5. System.out.println(json);
  6. }

Output

  1. {
  2. &quot;host_names&quot; : &quot;www.host1.com,www.host2.com&quot;
  3. }

This way you can serialize as comma separated string, and array of string while deserializing.

答案3

得分: 0

另一种使用 @JsonValue 注解的解决方案。将您的 bean 类定义如下:

  1. public class Server {
  2. @JsonProperty("host_names")
  3. private List<String> hostNames;
  4. @JsonValue
  5. public String hostNames() {
  6. return String.join(",", hostNames);
  7. }
  8. public List<String> getHostNames() {
  9. return hostNames;
  10. }
  11. public void setHostNames(List<String> hostNames) {
  12. this.hostNames = hostNames;
  13. }
  14. }

主方法

  1. public static void main(String[] args) throws JsonProcessingException {
  2. ObjectMapper mapper = new ObjectMapper();
  3. Server server = mapper.readValue("{\"host_names\":[\"www.host1.com\",\"www.host2.com\"]}", Server.class);
  4. String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(server);
  5. System.out.println(json);
  6. }

输出

  1. "www.host1.com,www.host2.com"

此方法仅在您的 bean 类没有其他要序列化的字段时才有用。

英文:

Another Solution using @JsonValue Annotation.
Define your bean class as:

  1. public class Server {
  2. @JsonProperty(&quot;host_names&quot;)
  3. private List&lt;String&gt; hostNames;
  4. @JsonValue
  5. public String hostNames() {
  6. return String.join(&quot;,&quot;, hostNames);
  7. }
  8. public List&lt;String&gt; getHostNames() {
  9. return hostNames;
  10. }
  11. public void setHostNames(List&lt;String&gt; hostNames) {
  12. this.hostNames = hostNames;
  13. }
  14. }

Main Method

  1. public static void main(String[] args) throws JsonProcessingException {
  2. ObjectMapper mapper = new ObjectMapper();
  3. Server server = mapper.readValue(&quot;{\&quot;host_names\&quot;:[\&quot;www.host1.com\&quot;,\&quot;www.host2.com\&quot;]}&quot;, Server.class);
  4. String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(server);
  5. System.out.println(json);
  6. }

Output

  1. &quot;www.host1.com,www.host2.com&quot;

This method will only be useful if your bean class has no other fields to serialize.

huangapple
  • 本文由 发表于 2020年8月30日 02:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定