简体   繁体   中英

Java String.valueOf(jsonArray) turns UTF-8 encoded content from jsonArray into question marks

I have this json array (of type JSONArray) containing utf-8 encoded strings:

    [{
      "success":true,
      "data":[
         {"moduleTitle":"تست",
          "title":"تست دو"}
       ],
       "status":200
    }]

And then I would like to get the string value of it.

String s = String.valueOf(jsonArray);

but the utf-8 strings will be turned into question marks like this:

"[{ "success":true, "data":[ {"moduleTitle":"???", "title":"??? ??"} ], "status":200 }]"

how can I make it show the strings correctly?

You are converting the data using the JVM's default charset, UTF-8 charset most probably is not on your server or local.

Try using this:

String s = String.valueOf(jsonArray);
String string2 = new String(s.getBytes(),"UTF-8");
return new ResponseEntity<String>(string2, HttpStatus.OK);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM