英文:
Clean string from backslashs and put the results into data strucutre
问题
我有这个字符串
"[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]"
如何清除这个字符串中的反斜杠并将实体放入数据结构中?
英文:
I have this string
"[{\"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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SOTest {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException, JSONException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]";
List<CustomClass> datas = mapper.readValue(jsonString, new TypeReference<List<CustomClass>>() {});
System.out.println(datas);
}
}
class CustomClass {
public String name;
public List<CustomClass> family;
public String status;
@Override
public String toString() {
return "Data [name=" + name + ", family=" + family + ", status=" + status + "]";
}
}
output
[Data [name=john, family=[], status=single], Data [name=david, family=[], status=marred]]
英文:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SOTest {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException, JSONException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]";
List<CustomClass> datas = mapper.readValue(jsonString, new TypeReference<List<CustomClass>>() {});
System.out.println(datas);
}
}
class CustomClass {
public String name;
public List<CustomClass> family;
public String status;
@Override
public String toString() {
return "Data [name=" + name + ", family=" + family + ", status=" + status + "]";
}
}
output
[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
在jshell控制台中编写,因此根据需要添加类等。
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
try {
String json = "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]";
json = json.replace("\\\"", "\"");
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
Person[] persons = mapper.readValue(json, Person[].class);
System.out.println(persons[0].name);
} catch (IOException e) {
e.printStackTrace();
}
static class Person {
public final String name;
public final String[] family;
public final String status;
@JsonCreator
public Person(
@JsonProperty("name") String name,
@JsonProperty("family") String[] family,
@JsonProperty("status") String status
) {
this.name = name;
this.family = family;
this.status = status;
}
}
英文:
Written in jshell console so you need to add classes etc as needed
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
try {
String json = "[{\"name\":\"john\",\"family\":[],\"status\":\"single\"},{\"name\":\"david\",\"family\":[],\"status\":\"marred\"}]";
json = json.replace("\\\"", "\"");
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
Person[] persons = mapper.readValue(json, Person[].class);
System.out.println(persons[0].name);
} catch (IOException e) {
e.printStackTrace();
}
static class Person {
public final String name;
public final String[] family;
public final String status;
@JsonCreator
public Person(
@JsonProperty("name") String name,
@JsonProperty("family") String[] family,
@JsonProperty("status") String status
) {
this.name = name;
this.family = family;
this.status = status;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论