0% found this document useful (0 votes)
14 views3 pages

Json

Uploaded by

gokulpradeep1908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Json

Uploaded by

gokulpradeep1908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Json :

Json(JavaScript Object Notation) is commonly used format for sending request


and getting response from Webapplication to the server.
It is very simple and useful to read and write object. Json object is mainly used
to transfer the data from webapplication to the server. This format send the data
in key and value pair. This format is used in REST full web services.

syntax:
[
{

"name":"uniq",
"age":23

},

"name":"ram",
"age":25

XML :

XML(Xtensible Markup Language) is a tag based language.it is used also used


to send data through request and get the data from response.
It is mainly used in soap web services.

Syntax:

<employee>
<name>uniq</name>
<age>21</age>
</employee>

Program for Converting json object into java object and java object into
xml object:

pojo class:

package Maven.Mavenfirst;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement // It is used to denote which object can be bind into XML object.
public class Employee {

private String name;


private int age;

public Employee() {} // for creating object

public Employee(String name, int age) { // for initializing object


this.name = name;
this.age = age;
}

@XmlElement // It is used to denote which attributes should be bind into xml


element.we specifically denote with this annotation.
public String getName() {
return name;
}

@XmlElement
public int getAge() {
return age;
}

public void setName(String name) {


this.name = name;
}

public void setAge(int age) {


this.age = age;
}

Object Converting class:

package Maven.Mavenfirst;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonToJavaToXML {

public static void javaToXML(Employee emp) {

try {
JAXBContext context=JAXBContext.newInstance(Employee.class);
Marshaller marshaller=context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(emp,System.out);

} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

public static void main(String[] args) {


String jsonvalue="{\"name\":\"uniq\",\"age\":20}";

ObjectMapper objectMapper=new ObjectMapper();


try {
Employee emp=objectMapper.readValue(jsonvalue, Employee.class);

System.out.println(emp.getAge()+" "+emp.getName());

javaToXML(emp);

} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

You might also like