I'm having trouble encoding java special characters returned from JSON data. I'm reading in a JSON file "raw.json", and certain names are giving me trouble.
For Example, I'm reading in the name 'Martin Perénya' from the JSON file and I'm to push it to a JsonArray of JsonObjects, with the special characters in the form of UNICODE like 'Martin Per\u00e9nya', but my code is interpreting it as 'Martin Perénya'. Can anyone help me with this please or even point me in the right direction? I'm new to this and not sure how to proceed, thanks.
package main.service;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Service {
public String getAll() throws FileNotFoundException {
JsonParser jsonParser = new JsonParser();
FileReader reader;
reader = new FileReader("raw.json");
JsonObject data = (JsonObject) jsonParser.parse(reader);
JsonArray issues = data.get("issues").getAsJsonArray();
JsonArray processedIssues = new JsonArray();
for(JsonElement i: issues) {
JsonObject object = new JsonObject();
JsonObject issue =(JsonObject) i;
JsonObject fields = issue.get("fields").getAsJsonObject();
//displayName
JsonObject assignee = fields.get("assignee").getAsJsonObject();
String name = assignee.get("displayName").getAsString();
if(assignee.size() !=0) {
if(name.length()==0) {
name = "None";
}
else {
name = assignee.get("displayName").getAsString();
}
}
object.addProperty("name", name);
processedIssues.add(object);
}
JsonObject returnData = new JsonObject();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = format.format( new Date());
returnData.addProperty("created", dateString);
returnData.add("featureList", processedIssues);
System.out.println("returnedData - " +returnData);
return returnData.toString();
}
}