清除字符串中的反斜杠并将结果放入数据结构中

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

Clean string from backslashs and put the results into data strucutre

问题

我有这个字符串

  1. "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]"

如何清除这个字符串中的反斜杠并将实体放入数据结构中?

英文:

I have this string

  1. "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]"

how can I clean up the backslashs from this string and put the entities into a data structure?

答案1

得分: 1

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import org.json.JSONArray;
  5. import org.json.JSONException;
  6. import com.fasterxml.jackson.core.JsonProcessingException;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.JsonMappingException;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. public class SOTest {
  11. public static void main(String[] args) throws JsonMappingException, JsonProcessingException, JSONException {
  12. ObjectMapper mapper = new ObjectMapper();
  13. String jsonString = "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]";
  14. List<CustomClass> datas = mapper.readValue(jsonString, new TypeReference<List<CustomClass>>() {});
  15. System.out.println(datas);
  16. }
  17. }
  18. class CustomClass {
  19. public String name;
  20. public List<CustomClass> family;
  21. public String status;
  22. @Override
  23. public String toString() {
  24. return "Data [name=" + name + ", family=" + family + ", status=" + status + "]";
  25. }
  26. }

output

  1. [Data [name=john, family=[], status=single], Data [name=david, family=[], status=marred]]
英文:
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import org.json.JSONArray;
  5. import org.json.JSONException;
  6. import com.fasterxml.jackson.core.JsonProcessingException;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.JsonMappingException;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. public class SOTest {
  11. public static void main(String[] args) throws JsonMappingException, JsonProcessingException, JSONException {
  12. ObjectMapper mapper = new ObjectMapper();
  13. String jsonString = &quot;[{\&quot;name\&quot;:\&quot;john\&quot;,\&quot;family\&quot;:[],\&quot;status\&quot;:\&quot;single\&quot;},{\&quot;name\&quot;:\&quot;david\&quot;,\&quot;family\&quot;:[],\&quot;status\&quot;:\&quot;marred\&quot;}]&quot;;
  14. List&lt;CustomClass&gt; datas = mapper.readValue(jsonString, new TypeReference&lt;List&lt;CustomClass&gt;&gt;() {});
  15. System.out.println(datas);
  16. }
  17. }
  18. class CustomClass {
  19. public String name;
  20. public List&lt;CustomClass&gt; family;
  21. public String status;
  22. @Override
  23. public String toString() {
  24. return &quot;Data [name=&quot; + name + &quot;, family=&quot; + family + &quot;, status=&quot; + status + &quot;]&quot;;
  25. }
  26. }

output

  1. [Data [name=john, family=[], status=single], Data [name=david, family=[], status=marred]]

答案2

得分: 0

以下是翻译好的内容:

这个字符串看起来没问题。转义是为了在 Java 中使用这个字符串。

针对你的任务,可以使用像 Jackson 这样的库。

英文:

The string looks okay. The escaping is done to use the string within java.

Use a library like Jackson. For your task.

答案3

得分: -1

  1. jshell控制台中编写因此根据需要添加类等
  2. import com.fasterxml.jackson.annotation.JsonCreator;
  3. import com.fasterxml.jackson.annotation.JsonProperty;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import java.io.IOException;
  6. try {
  7. String json = "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]";
  8. json = json.replace("\\\"", "\"");
  9. System.out.println(json);
  10. ObjectMapper mapper = new ObjectMapper();
  11. Person[] persons = mapper.readValue(json, Person[].class);
  12. System.out.println(persons[0].name);
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. static class Person {
  17. public final String name;
  18. public final String[] family;
  19. public final String status;
  20. @JsonCreator
  21. public Person(
  22. @JsonProperty("name") String name,
  23. @JsonProperty("family") String[] family,
  24. @JsonProperty("status") String status
  25. ) {
  26. this.name = name;
  27. this.family = family;
  28. this.status = status;
  29. }
  30. }
英文:

Written in jshell console so you need to add classes etc as needed

  1. import com.fasterxml.jackson.annotation.JsonCreator;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.io.IOException;
  5. try {
  6. String json = &quot;[{\&quot;name\&quot;:\&quot;john\&quot;,\&quot;family\&quot;:[],\&quot;status\&quot;:\&quot;single\&quot;},{\&quot;name\&quot;:\&quot;david\&quot;,\&quot;family\&quot;:[],\&quot;status\&quot;:\&quot;marred\&quot;}]&quot;;
  7. json = json.replace(&quot;\\\&quot;&quot;, &quot;\&quot;&quot;);
  8. System.out.println(json);
  9. ObjectMapper mapper = new ObjectMapper();
  10. Person[] persons = mapper.readValue(json, Person[].class);
  11. System.out.println(persons[0].name);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. static class Person {
  16. public final String name;
  17. public final String[] family;
  18. public final String status;
  19. @JsonCreator
  20. public Person(
  21. @JsonProperty(&quot;name&quot;) String name,
  22. @JsonProperty(&quot;family&quot;) String[] family,
  23. @JsonProperty(&quot;status&quot;) String status
  24. ) {
  25. this.name = name;
  26. this.family = family;
  27. this.status = status;
  28. }
  29. }

huangapple
  • 本文由 发表于 2020年10月9日 15:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64275552.html
匿名

发表评论

匿名网友

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

确定