How to transfer jsonobject to string
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!