Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to transfer jsonobject to string

Shulou Source: shulou.com Published: 2022-05-31 10:37:47 09月30日 Update

This article mainly explains "how to change jsonobject to string". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to convert jsonobject to string".

Step 1: the console enters the information and receives the returned results

Scanner sc = new Scanner (System.in); while (true) String content = ""; / / console input information content = sc.nextLine (); / / receive the returned result String result = TuLingApiUtil.getResult (content)

Step 2: convert a string in json format to a json object

JsonObject json = new JsonParser () .parse (result) .getAsJsonObject ()

Step 3: get the contents of the text key and convert it to string

String back = json.get ("text") .toString

Output

System.out.println (back)

It should be noted that the null value is not output after jsonobject is converted to string.

For example

JSONObject jsonObject = new JSONObject (); jsonObject.add ("name", null); String str = jsonObject.toString (); / / {} str = JSONObject.toJSONString (jsonObject, SerializerFeature.WriteMapNullValue); / / {"name", null} Thank you for your reading. This is the content of "how to transfer jsonobject to string". After the study of this article, I believe you have a deeper understanding of how to transfer jsonobject to string, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Content learning information console results control input output characters strings objects that is ideas situations articles more formats knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux Redmi macOS Shulou Technology