0
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("testCollection");

Document document= Document.parse("{'author' : 'Punama','answer' : 'อาม่า(เจ้าของ)'}");

collection.insertOne(document);         
System.out.println("Record inserted.....");         

JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build();
FindIterable<Document> findIterable=collection.find();

for(Document doc: findIterable){
    System.out.println(doc.toJson(settings));
}

This the output of doc.toJson(settings):

{ "_id" : ObjectId("5bc70756d47228167ce6f2a1"), "author" : "Punama", "answer" : "อาม\u0e48า(เจ\u0e49าของ)" }

Expected output:

{ "_id" : ObjectId("5bc70756d47228167ce6f2a1"), "author" : "Punama", "answer" : "อาม่า(เจ้าของ)" }

Please help me to do this. toJson(-) is not printing exact output. It will print ASCII codes. Need output like "อาม่า(เจ้าของ)".

Below code solve my problem but this legacy method(JSONSerializers.getLegacy()) deprecated in mongo java driver 3.8 version.

String afterSerialization=JSONSerializers.getLegacy().serialize(doc);

  • Possible duplicate of [Printing unicode to console](https://stackoverflow.com/questions/34360830/printing-unicode-to-console) – dnickless Oct 17 '18 at 11:10
  • https://stackoverflow.com/questions/44878530/print-unicode-character-in-java – dnickless Oct 17 '18 at 11:10
  • https://stackoverflow.com/questions/20386335/printing-out-unicode-from-java-code-issue-in-windows-console – dnickless Oct 17 '18 at 11:10
  • https://stackoverflow.com/questions/20898904/java-in-console-thai-text-is-printing-as-some-strange-character – dnickless Oct 17 '18 at 11:13
  • Thanks Dnickless but my requirement is different. – Bhalchandra Giri Oct 18 '18 at 09:03
  • When i use single word (อาม\u0e48า(เจ\u0e49าของ)), it converted properly. I need to parse Document object into json format then into String then it will not covert, still showing same like อาม\u0e48า(เจ\u0e49าของ). – Bhalchandra Giri Oct 19 '18 at 05:22

1 Answers1

0

Sorry for late post answer. We have to use org.json.jar to get rid of thai character issue.

while (cursor.hasNext()) {

        Document d=cursor.next();                   
        JSONObject object=new JSONObject(d); //import org.json.JSONObject;
        System.out.println(object.toString());
    }

This will give proper output.