【问题标题】:Clean string from backslashs and put the results into data strucutre从反斜杠中清除字符串并将结果放入数据结构中
【发布时间】:2020-10-09 07:18:22
【问题描述】:

我有这个字符串

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

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

【问题讨论】:

标签: java string data-structures


【解决方案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 + "]";
    }
}

输出

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

【讨论】:

  • 在尝试将对象保存到 elasticsearch 时出现以下错误:nested: ElasticsearchException[Elasticsearch exception [type=illegal_state_exception, reason=Can't get text on a START_OBJECT at 1:731]];
  • 这可能是一个不同的问题,你可以粘贴你如何将它传递给弹性搜索的代码吗?
  • @Catalina 这是我在谷歌搜索时发现的“”消息“无法在 START_OBJECT 上获取文本”意味着 Elasticsearch 期望输入“字符串”类型的条目,但您试图给出一个对象作为输入。""
  • elasticserch 好像只接受字符串存储?
  • @Catalina 在上面的代码之后,试试这个 String str = mapper.writeValueAsString(datas);然后发送尝试发送到 Elastic Search,P.S 我工作不多或对 ES 了解不多
【解决方案2】:

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

使用像Jackson 这样的库。为了你的任务。

【讨论】:

    【解决方案3】:

    在 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;
        }
    }
    

    【讨论】:

    • 抱歉,您的 replace() 是一个空操作 - 不需要替换任何东西!只需尝试用 .replace() 注释该行,看看它是否有任何区别......
    • 实际上给出了问题和测试数据,它工作得很好。但我从随后的讨论中看到,这并不完全是他所追求的,而只是解析一个 json 响应。无论如何我都会留下它,遇到存储在数据库或类似文件中的数据并非完全不可能......
    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多