Java中JSON未知的字符串 [英] String unknown to JSON in Java

查看:150
本文介绍了Java中JSON未知的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自URL的字符串,我知道格式化为JSON,但我不知道可以更改的字段和大小。我尝试将其解析为JSON对象,以便能够迭代它。字符串看起来像这样:

I've got a string from an URL which I know is formatted as a JSON but I don't know the field that can change and the size. I try to parse it into a JSON Object to be able to iterate on it. the string look like that:


{_ index:my_index,_ type:my_type,_ id: 67663551-ed47-11e4-99b8-93bceafc1a4e, _版本:4, 发现:真实的, term_vectors:{ 说明:{ field_statistics:{ sum_doc_freq:573723, doc_count:13992, sum_ttf:626963}, 术语:{ 1930:{ doc_freq:391, TTF:398, term_freq:1, 标记:[{ 位置:1, start_offset :7, end_offset:11}]}, 1er酒店:{ doc_freq:1406, TTF:1505, term_freq:1, 标记:[{ 位置:11 start_offset :64, end_offset:67}]}, 2em的:{ doc_freq:5 TTF:5 term_freq:1, 标记:[{ 位置:18, start_offset :104, end_offset:107}]}, 骨:{ doc_freq:132, TTF:138, term_freq:1, 标记:[{ 位置:26, start_offset :158, end_offset:163}]}, 公共汽车:{ doc_freq:531, TTF:556, term_freq:1, 标记:[{ 位置:31, start_offset :197, end_offset:200}]}, 洞:{ doc_freq:2176, TTF:2300, term_freq:1, 标记:[{ 位置:10, start_offset :59, end_offset:63}]}, chambr:{ doc_freq:5087, TTF:6194, term_freq:2,令牌 :[{ 位置 :9,start_offset :51, end_offset :58},{ 位置 :19, start_offset :109, end_offset :116}]}, 硬币: { doc_freq:3385, TTF:3765, term_freq:2 标记:[{ 位置:4 start_offset:24, end_offset:28},{ 位置:12 start_offset:70, end_offset:74}]}, comerc:{ doc_freq:3226, TTF:3265, term_freq:1, 标记:[{ 位置:29 start_offset:182, end_offset:190}]}, cuisin:{ doc_freq:8241, TTF:8537, term_freq:2 标记:[{ 位置:5 start_offset:29, end_offset:36},{ 位置:13, start_offset:75, end_offset:82}]}, 不要:{ doc_freq:20, TTF :20, term_freq:1, 标记:[{ 位置:23, start_offset:139, end_offset:145}]}, doubl:{ doc_freq:1299, TTF :1417, term_freq:1, 标记:[{ 位置:20, start_offset:117, end_offset:123}]}, douch:{ doc_freq:3687, TTF :3854, term_freq:2 标记:[{ 位置:7, start_offset:41, end_offset:47},{ 位置:16, start_offset:94, end_offset :100}]}, ENTR:{ doc_freq:3343, TTF:3540, term_freq:1, 标记:[{POSITI上的 :21, start_offset :124, end_offset :130}]}, 展览会 :{ doc_freq :608, TTF :618, term_freq :1, 标记 :[{位置 :27, start_offset :164, end_offset :174}]}, LILE :{ doc_freq :9535, TTF :13834,term_freq :1, 标记 :[{位置 :2,start_offset :12, end_offset :17}]}, 位置 :{ doc_freq :3054, TTF :3387, term_freq :1, 标记 :[{位置 :25, start_offset :149, end_offset :157}]},的 Maison :{ doc_freq :2032, TTF :3087, term_freq :1, 标记 :[{位置 :0, start_offset :0,end_offset :6}]}, 地铁 :{ doc_freq :4695, TTF :4928, term_freq :1, 标记 :[{位置 :30, start_offset :191, end_offset :196}]}, pouvant :{ doc_freq :84, TTF :91, term_freq :1, 标记 :[{位置 :22, start_offset :131, end_offset :138}]}, proch :{ doc_freq :2566, TTF :2754, term_freq :1, 标记 :[{位置 :28, start_offset :175, end_offset :181}]}, RDC :{ doc_freq :457, TTF :464, term_freq :1, 标记 :[{位置 :3,start_offset :18, end_offset :21}]}, SDB :{ doc_freq :1008, TTF :1019, term_freq :2,标记: [{ 位置:6, start_offset:37, end_offset:40},{ 位置:15, start_offset:90, end_offset:93}]}, sejou:{doc_freq :5837, TTF :5993, term_freq :1, 标记 :[{ 位置 :14, start_offset :83, end_offset :89}]}, WC :{ doc_freq :5543, TTF :5706, term_freq :2,标记 :[{ 位置 :8中, start_offset :48, end_offset :50},{ 位置 :17, start_offset :101,end_offset:103}]}}}}}

{"_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可以帮助我吗?

I don't succeed to parse it into a JSON could help me?

推荐答案

我对绘图的最佳体验是com.fasterxml.jackson

My best experience with mapping is com.fasterxml.jackson

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

use to make a Json String from your class (whatever the subclasses, as long as all have appropriate getters and setters, and an empty (public) constructor)

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字符串加载类

to load the class from the json strings

如果你有一个Json对象并且没有它的Java字段,你可能想尝试 http://timboudreau.com/blog/json/read 生成Java代码。

If you have a Json object and don't have the Java fields for it, you might want to try http://timboudreau.com/blog/json/read to generate the Java code.

这篇关于Java中JSON未知的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆