python how to convert object to json code example
Example 1: python json string to object
import json
x = '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)
print(y["age"])
Example 2: python object to json
jsonStr = json.dumps(myobject.__dict__)
Example 3: convert class object to json python
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
laptop1 = Laptop()
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
jsonStr = json.dumps(laptop1.__dict__)
print(jsonStr)
Example 4: how to convert json to java object
Jackson Data-bind depdendency that take care of
Converting between
JSON to Java Object
and
Java Object to JSON
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>