Python equivalent json.dumps

Python Code:

import json jsonvalue = json.dumps({"key":key_string,"value":unsafe_string}) cursor_mysql.execute("""UPDATE data SET json = %s WHERE id = %s""", (jsonvalue, somerowid)) 

jsonvalue is a valid json created by json.dumps , even if unsafe_string contains unicodes. Is there a similar function in Java?

+4
source share
3 answers

Do you need json-simple or google gson

Encoding using json-simple :

  JSONObject obj=new JSONObject(); obj.put("name","foo"); obj.put("num",new Integer(100)); obj.put("balance",new Double(1000.21)); obj.put("is_vip",new Boolean(true)); obj.put("nickname",null); System.out.print(obj); Result: {"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"} 

Similar to Decoding .

+7
source

Using Jackson :

 ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(dst, obj); // where 'dst' can be File, OutputStream or Writer 
+2
source

Using flexjson :

 String json = new flexjson.JSONSerializer().serialize(obj); 
+1
source

Source: https://habr.com/ru/post/1386450/


All Articles