6

I'm trying to parse JSON using json-simple-1.1.1

public static void main(String[] args) throws ParseException, IOException{

    BufferedReader buff = new BufferedReader(new FileReader("src/qqqqqqqq/json"));

    String line = null;

    while((line = buff.readLine()) != null){

        JSONParser parser = new JSONParser();
        Object obj = (Object) parser.parse(line);
        JSONObject jsonObj = (JSONObject) obj;

        System.out.println((String)jsonObj.get("name"));
    }
}

My JSON source file using UTF-8 without BOM

{"name":"ą"}
{"name":"ć"}
{"name":"ń"}
{"name":"ź"}
{"name":"ż"}
{"name":"ó"}

Output from println:

Ä…
ć
Ĺ„
Ĺş
ĹĽ
Ăł

What I am doing wrong?

1

1 Answer 1

6

A FileReader uses the default charset which must not be UTF-8.

Use

new BufferedReader(new InputStreamReader(new FileInputStream("src/qqqqqqqq/json"), "UTF-8"));

instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.