String unknown to JSON in Java

2019-03-03 13:36发布

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","_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}]}}}}}

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

1条回答
倾城 Initia
2楼-- · 2019-03-03 14:22

My best experience with mapping is com.fasterxml.jackson

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();
}

use

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

to load the class from the json strings

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.

查看更多
登录 后发表回答