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

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

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 = &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;;
		List&lt;CustomClass&gt; datas = mapper.readValue(jsonString, new TypeReference&lt;List&lt;CustomClass&gt;&gt;() {});
		System.out.println(datas);
	}
}

class CustomClass {
	public String name;
	public List&lt;CustomClass&gt; family;
	public String status;

	@Override
	public String toString() {
		return &quot;Data [name=&quot; + name + &quot;, family=&quot; + family + &quot;, status=&quot; + status + &quot;]&quot;;
	}
}

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 = &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;;
    json = json.replace(&quot;\\\&quot;&quot;, &quot;\&quot;&quot;);
    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(&quot;name&quot;) String name,
            @JsonProperty(&quot;family&quot;) String[] family,
            @JsonProperty(&quot;status&quot;) String status
    ) {
        this.name = name;
        this.family = family;
        this.status = status;
    }
}

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:

确定