Java 中 JSON 未知的字符串

String unknown to JSON in Java

提问人:mel 提问时间:6/24/2015 最后编辑:Brian Tompsett - 汤莱恩mel 更新时间:11/17/2023 访问量:184

问:

我有一个来自 URL 的字符串,我知道它的格式为 JSON,但我不知道可以更改的字段和大小。我尝试将其解析为JSON对象,以便能够对其进行迭代。字符串如下所示:

{“_index”:“my_index”,“_type”:“my_type”,“_id”:“67663551-ed47-11e4-99b8-93bceafc1a4e”,“_version”:4,“found”:true,“term_vectors”:{“description”:{“field_statistics”:{“sum_doc_freq”:573723,“doc_count”:13992,“sum_ttf”:626963},“terms”:{“1930”:{“doc_freq”:391,“ttf”:398,“term_freq”:1,“tokens”:[{“position”:1,“start_offset”:7,“end_offset”:11}]},“1er”:{“doc_freq”:1406,“ttf”:1505,“term_freq”:1,“tokens”:[{“position”:11,“start_offset”:64,“end_offset”:67}]},“2em”:{“doc_freq”:5,“ttf”:5,“term_freq”:1,“tokens”:[{“position”:18,“start_offset”:104,“end_offset”:107}]},“bone”:{“doc_freq”:132,“ttf”:138,“term_freq”:1,“tokens”:[{“position”:26,“start_offset”:158,“end_offset”:163}]},“bus”:{“doc_freq”:531,“ttf”:556,“term_freq”:1,“tokens”:[{“position”:31,“start_offset”:197,“end_offset”:200}]},“cave”:{“doc_freq”:2176,“ttf”:2300,“term_freq”:1,“tokens”:[{“position”:10,“start_offset”:59,“end_offset”:63}]},“chambr”:{“doc_freq”:5087,“ttf”:6194,“term_freq”:2,“tokens”:[{“position”:9,“start_offset”:51,“end_offset”:58},{“position”:19,“start_offset”:109,“end_offset”:116}]},“coin”:{“doc_freq”:3385,“ttf”:3765,“term_freq”:2,“tokens”:[{“position”:4,“start_offset”:24,“end_offset”:28},{“position”:12,“start_offset”:70,“end_offset”:74}]},“comerc”:{“doc_freq”:3226,“ttf”:3265,“term_freq”:1,“tokens”:[{“position”:29,“start_offset”:182,“end_offset”:190}]},“cuisin”:{“doc_freq”:8241,“ttf”:8537,“term_freq”:2,“tokens”:[{“position”:5,“start_offset”:29,“end_offset”:36},{“position”:13,“start_offset”:75,“end_offset”:82}]},“don”:{“doc_freq”:20,“ttf”:20,“term_freq”:1,“tokens”:[{“position”:23,“start_offset”:139,“end_offset”:145}]},“doubl”:{“doc_freq”:1299,“ttf”:1417,“term_freq”:1,“tokens”:[{“position”:20,“start_offset”:117,“end_offset”:123}]},“douch”:{“doc_freq”:3687,“ttf”:3854,“term_freq”:2,“tokens”:[{“position”:7,“start_offset”:41,“end_offset”:47},{“position”:16,“start_offset”:94,“end_offset”:100}]},“entr”:{“doc_freq”:3343,“ttf”:3540,“term_freq”:1,“tokens”:[{“position”:21,“start_offset”:124,“end_offset”:130}]},“expos”:{“doc_freq”:608,“ttf”:618,“term_freq”:1,“tokens”:[{“position”:27,“start_offset”:164,“end_offset”:174}]},“lile”:{“doc_freq”:9535,“ttf”:13834,“term_freq”:1,“tokens”:[{“position”:2,“start_offset”:12,“end_offset”:17}]},“location”:{“doc_freq”:3054,“ttf”:3387,“term_freq”:1,“tokens”:[{“position”:25,“start_offset”:149,“end_offset”:157}]},“maison”:{“doc_freq”:2032,“ttf”:3087,“term_freq”:1,“tokens”:[{“position”:0,“start_offset”:0,“end_offset”:6}]},“metro”:{“doc_freq”:4695,“ttf”:4928,“term_freq”:1,“tokens”:[{“position”:30,“start_offset”:191,“end_offset”:196}]},“pouvant”:{“doc_freq”:84,“ttf”:91,“term_freq”:1,“tokens”:[{“position”:22,“start_offset”:131,“end_offset”:138}]},“proch”:{“doc_freq”:2566,“ttf”:2754,“term_freq”:1,“tokens”:[{“position”:28,“start_offset”:175,“end_offset”:181}]},“rdc”:{“doc_freq”:457,“ttf”:464,“term_freq”:1,“tokens”:[{“position”:3,“start_offset”:18,“end_offset”:21}]},“sdb”:{“doc_freq”:1008,“ttf”:1019,“term_freq”:2,“tokens”:[{“position”:6,“start_offset”:37,“end_offset”:40},{“position”:15,“start_offset”:90,“end_offset”:93}]},“sejou”:{“doc_freq”:5837,“ttf”:5993,“term_freq”:1,“tokens”:[{“position”:14,“start_offset”:83,“end_offset”:89}]},“wc”:{“doc_freq”:5543,“ttf”:5706,“term_freq”:2,“tokens”:[{“position”:8,“start_offset”:48,“end_offset”:50},{“position”:17,“start_offset”:101,“end_offset”:103}]}}}}}

我没有成功将其解析为JSON可以帮助我吗?

Java JSON 字符串 解析

评论

0赞 duffy356 6/24/2015
你如何解析它?
5赞 light_303 6/24/2015
你有没有尝试过像杰克逊这样的JSON库?他们能完成您需要的所有繁重工作。
0赞 Marged 6/24/2015
您需要 JSON 的哪一部分?全部或仅一个百分比?并请澄清其含义field that can change and the size

答:

2赞 Danielson 6/24/2015 #1

我在映射方面的最佳体验是 com.fasterxml.jackson

用于从你的类中创建一个 Json 字符串(无论子类是什么,只要所有子类都有适当的 getter 和 setter,以及一个空的(公共)构造函数)

public String toJson() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    StringWriter stringEmp = new StringWriter();
    try {
        objectMapper.writeValue(stringEmp, this);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringEmp.toString();
}

public static ThisClass initFromJson(final String json) {
    final ObjectMapper mapper = new ObjectMapper();
    ThisClass item;
    try {
        item = mapper.readValue(json, ThisClass.class);
    } catch (IOException e) {
        return null;
    }

    return item;
}

从 JSON 字符串加载类

如果您有一个 Json 对象,但没有该对象的 Java 字段,则可能需要尝试 https://json2csharp.com/code-converters/json-to-pojo 生成 Java 代码。

评论

0赞 Marged 6/24/2015
你不认为他需要从JSON转到Java对象吗?
0赞 Danielson 6/24/2015
@Marged是的,我愿意,这就是第二种方法的用途。第一个是相反的。但是,当您发送 JSON.toString() 时,Objectmapper 会将其映射到 Java 类
0赞 Marged 6/24/2015
但是你需要定义这个类,所以这意味着他必须事先创建这样一个类。据我了解他的问题,他收到了 JSON 并且没有自己创建它,因此没有相应的对象。
0赞 Danielson 6/24/2015
@Marged极好的观点!要从 JSON 生成 Java 类,请尝试 timboudreau.com/blog/json/read
1赞 Stephan 11/16/2023
答案中的链接已断开。