Java Classes to Generate the Given Json String

Java classes to generate the given JSON string

This should be doable using a custom deserializer.

A relationship in my opinion should be modeled as a proper java class with proper names. Note that the constructor takes a JSONNode as an argument and that I have left our any getters and setters:

public class Relationship {

private final String id1;
private final String id2;
private final Relation relation;
private final boolean careTaker;
private final boolean liveTogether;

public Relationship(JsonNode base) {
this.id1 = base.get(0).asText();
this.id2 = base.get(2).asText();
this.relation = Relation.valueOf(base.get(1).asText());
this.careTaker = base.get(3).get("careTaker").asBoolean();
this.liveTogether = base.get(3).get("liveTogether").asBoolean();
}

public enum Relation {
PARENT,
SPOUSE;
}

}

We also need a class which stores the collection. This is the one that you would deserialize the top level object into (again leaving out getters and setters):

@JsonDeserialize( using = FamillyRelationshipsDeserializer.class )
public class FamillyRelationships {

public List<Relationship> familyRelationships = new ArrayList<>();

}

Finally we need to implement the actual JsonDeserializer referenced in the above class. It should look something like the following. I used this question as a reference:

class FamillyRelationshipsDeserializer extends JsonDeserializer<FamillyRelationships> {
@Override
public FamillyRelationships deserialize(JsonParser jp, DeserializationContext ctxt) throws
IOException, JsonProcessingException {
FamillyRelationships relationships = new FamillyRelationships();
JsonNode node = jp.readValueAsTree();
JsonNode rels = node.get("familyRelationships");

for (int i = 0; i < rels.size(); i++) {
relationships.familyRelationships.add(new Relationship(rels.get(i));
}

return relationships;
}
}

I hope this helps, I haven't actually tested any of this, it probably will not even compile, but the principles should be right. I have also assumed that the JSON is of the format you supplied. If that's not a guarantee then you will need to make the proper checks and deal with any deviations.

If you also want to be able to serialize everything then you will need to implement JsonSerializer see this question for more details.

How to create JAVA object from JSON string?

You can use the following way.

gson.toJson(obj); // is used to convert object to JSON

if you want to convert JSON to Java Object then you can use

gson.fromJson(json, Car.class);

Ex.

     public class Car {
public String brand = null;
public int doors = 0;
// add getter and setter

}

String json = "{\"brand\":\"Jeep\", \"doors\": 3}";

Gson gson = new Gson();

Car car = gson.fromJson(json, Car.class);

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

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 (:

How can I write Java Model Class for Json Schema?

When creating Java POJO for a json schema you just need to map the fields. For your problem we can something like this.

public class SquadDto{
List<Squad> squads;
}

public class Squad{
List<Coach> coach;
List<Goalkeeper> goalkeepers;
List<Defender> defenders;
List<Midfielder> midfielders;
List<Attacker> attackers;
}
public class Coach{
long id;
String name;
String ccode;
String cname;
String role;
}

Similarly we can make classes for GoalKeeper,.. ,so that they map the fields in JSON.



Related Topics



Leave a reply



Submit