0

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();
    }

}
Razneesh
  • 1,147
  • 3
  • 13
  • 29
Lynskey08
  • 47
  • 8
  • 1
    implement this solution https://stackoverflow.com/a/37021602/8035260 – jose praveen Mar 19 '20 at 12:19
  • 5
    @JosePraveen: please don't. Read any of the countless higher-votes answers to that question to understand what is going on instead of mindlessly encoding and decoding a string with basically random encodings, that's not an appropriate solution for this problem. – Joachim Sauer Mar 19 '20 at 12:23
  • 5
    `FileReader` will use the platform default encoding, but JSON is *usually* written with the UTF-8 encoding. Use a `FileInputStream` and an `InputStreamReader` and construct the second one while passing in the `"UTF-8"` string to tell it about the encoding. – Joachim Sauer Mar 19 '20 at 12:25
  • 1
    The good thing about the link to that "solution" was that there were a lot of answers that needed downvoting... – Kayaman Mar 19 '20 at 12:48
  • Does this answer your question? [How to read write this in utf-8?](https://stackoverflow.com/questions/13350676/how-to-read-write-this-in-utf-8) – Ole V.V. Mar 20 '20 at 05:03
  • 2
    [1] I hesitate to mark your question as a duplicate of [Java - é becomes é - How to fix it](https://stackoverflow.com/q/16208517/2985643), but take a look since it might be helpful. [2] Update your question to show some sample data in **raw.json** so that others can try to replicate/resolve your issue. – skomisa Mar 21 '20 at 04:46

0 Answers0