gpt4 book ai didi

java - Java 中 JSON 未知的字符串

转载 作者:行者123 更新时间:2023-12-02 04:34:22 25 4
gpt4 key购买 nike

我从 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 可以帮助我吗?

最佳答案

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

用于从您的类创建 Json 字符串(无论子类如何,只要都有适当的 getter 和 setter,以及一个空(公共(public))构造函数)

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 字段,您可能需要尝试 http://timboudreau.com/blog/json/read生成 Java 代码。

关于java - Java 中 JSON 未知的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31022068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com