HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

JSON extraction

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
jsonextractionstackoverflow

Problem

Given the code below, is there a better way to essentially create different native objects without changing the native constructor or the nativeList.json file? The only option I can see is adding a String nativeType variable to the constructor and creating the native object based on that the type passed. So to create all the types of natives I would create an array and just loop through that, each time passing the same native name and a different type.

I would like to avoid this because I believe there are like 20 - 25 different types, so I was wondering if you had any suggestions.

Entity.java

```
package models;

public class Entity {

private int xPos, yPos; // used to keep track of the entity's position on the board
private String imgFilePath; // used to show image file for entity
private int entityNum; // used to uniquely identify a character
private String entityName; // used for entity name

/ Getter Methods /
// Get the entity's x position on the board
public int getxPos() {
return xPos;
}

// Get the entity's y position on the board
public int getyPos() {
return yPos;
}

// Get the entity's image
public String getImgFilePath() {
return imgFilePath;
}

// Get entity number
public int getEntityNum() {
return entityNum;
}

// Get the entity's name
public String getEntityName() {
return entityName;
}

/ Setter Methods /
// Set the entity's x position on the board
public void setxPos(int xPos) {
this.xPos = xPos;
}

// Set the entity's y position on the board
public void setyPos(int yPos) {
this.yPos = yPos;
}

// Set the entity's image
public void setImgFilePath(String imgFilePath) {
this.imgFilePath = imgFilePath;
}

// Set the entity's unique identifying number
public void setEntityNum(int entityNum) {
this.entityNum = entityNum;
}

Solution

In general, parsing JSON files manually as JSONObject classes etc. is not very nice or productive. You need to use some object mapper. I can recommend Jackson for you, it's quite nice. See, for example, this example: http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial

Basically your parsing code would then boil down to this part of the tutorial:

//read json file data to String
byte[] jsonData = Files.readAllBytes(Paths.get("employee.txt"));

//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();

//convert json string to object
Employee emp = objectMapper.readValue(jsonData, Employee.class);

System.out.println("Employee Object\n"+emp);


Just replace "Employee" with "Native" etc...

Code Snippets

//read json file data to String
byte[] jsonData = Files.readAllBytes(Paths.get("employee.txt"));

//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();

//convert json string to object
Employee emp = objectMapper.readValue(jsonData, Employee.class);

System.out.println("Employee Object\n"+emp);

Context

StackExchange Code Review Q#82392, answer score: 3

Revisions (0)

No revisions yet.