object to json javascript
var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);
object to json javascript
var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);
java serialize object to json
public class someClass {
//your lcode and logic...
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(MyAwesomeClass.class, new MyAwesomeSerializer());
mapper.registerModule(module);
jsonNode = mapper.convertValue(myAwesomeObject, JsonNode.class);
//... your code and logic
}
public class MyAwesomeSerializer extends StdSerializer<MyAwesomeClass> {
public MyAwesomeSerializer() {
super(MyAwesomeClass.class);
}
@Override
public void serialize(MyAwesomeClass myAwesomeClass, JsonGenerator jgen, SerializerProvider provider) throws IOException {
ObjectMapper mapper = new ObjectMapper();
jgen.writeStartObject();
jgen.writeStringField("name", myAwesomeClass.getName());
jgen.writeStringField("age", myAwesomeClass.getAge());
jgen.writeArrayFieldStart("hobbies");
for (Hobby hobby : myAwesomeClass.getHobbies()) {
jgen.writeObject(mapper.convertValue(hobby, JsonNode.class));
}
jgen.writeEndArray();
jgen.writeEndObject();
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us