0

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?

3
  • How do you parse it? Commented Jun 24, 2015 at 8:58
  • 5
    Did you try any JSON libraries like Jackson? They do all the heavy lifting you need. Commented Jun 24, 2015 at 8:58
  • which part of the JSON do you need ? All or only a percentage ? And please clarify what's the meaning of field that can change and the size Commented Jun 24, 2015 at 12:52

1 Answer 1

2

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 https://json2csharp.com/code-converters/json-to-pojo to generate the Java code.

Sign up to request clarification or add additional context in comments.

6 Comments

Don't you think he needs to go from JSON to Java object ?
@Marged yes I do, that's what the second method is for. The first one is the other way around. But when you send JSON.toString() the Objectmapper maps it to a Java Class
But you need to have that class defined, so this would mean that he has to create such a class beforehand. As I understand his problem he receives the JSON and did not create it on his own, so there is no corresponding object.
@Marged excellent point! To generate Java Class from JSON, try timboudreau.com/blog/json/read
The link in answer is broken.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.