JSON to POJO: How to generate abstract java class names from unique JSON fields?

I want to generate Java Classes using a given JSON schema

Using the WikiMedia API, I receive the page info from a search

JSON schema:

(The small part that gives me problems)

"pages": {
            "352214": {
                "pageid": 352214,
                "ns": 0,
                "title": "Montellano",
                "extract": "Montellano es un municipio español..."
            }
        }

As you can see the field 352214 is unique to this search which gives a problem using the POJO.


I'm using jsonschema2pojo with Jackson2 as Annotation Style

POJO


import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"pageid",
"ns",
"title",
"extract"
})
public class _352214 {

@JsonProperty("pageid")
private Integer pageid;
@JsonProperty("ns")
private Integer ns;
@JsonProperty("title")
private String title;
@JsonProperty("extract")
private String extract;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("pageid")
public Integer getPageid() {
return pageid;
}

@JsonProperty("pageid")
public void setPageid(Integer pageid) {
this.pageid = pageid;
}

@JsonProperty("ns")
public Integer getNs() {
return ns;
}

@JsonProperty("ns")
public void setNs(Integer ns) {
this.ns = ns;
}

@JsonProperty("title")
public String getTitle() {
return title;
}

@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}

@JsonProperty("extract")
public String getExtract() {
return extract;
}

@JsonProperty("extract")
public void setExtract(String extract) {
this.extract = extract;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

_352214 class will suit only for the wiki search with the same field name, whenever I make another search and this field changes, it crashes. How do I solve this?

Upvotes: 1

Views: 717

Answers (1)

S.Daineko
S.Daineko

Reputation: 2003

There is no standard way to parse situations like this(situations when you don't know field name). As an option you can manually parse your file using Jackson:

public void parseWikiResponse(String wikiResponse)  {
       JsonFactory jsonFactory = new JsonFactory();

       ObjectMapper mapper = new ObjectMapper(jsonFactory);

       JsonNode jsonNodes = mapper.readTree(wikiResponse);  

       Iterator<Map.Entry<String,JsonNode>> fieldsIterator = jsonNodes.fields();

       while (fieldsIterator.hasNext()) {

           Map.Entry<String,JsonNode> field = fieldsIterator.next();

           /* Here you can find your field with unknown name using regExp eg */
           field.getKey(); 
       }
}

If you want only for parsing this approach should solve the problem.

There is a similar question on this topic:

Parsing JSON in Java without knowing JSON format

Hope something helped (:

Upvotes: 1

Related Questions