简体   繁体   中英

Converting JSON object to string in JAXB

Am writing an Webservice endpoint which will produce JSON and XML response. Here i would like to convert an object to JSON and setting it to string. When i try to do with the below code

Webservice Endpoint

package com.test1;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

            import org.json.JSONArray;
            import org.json.JSONException;
            import org.json.JSONObject;`


            @Path("v1")
            public class Impl1 {
                   @POST
                   @Path("method")
                   @Produces({"text/xml","application/json"})
                   public XML method() throws JSONException
                   {
                          //JSONObject obj = new JSONObject();
                          JSONArray ary = new JSONArray();
                          Main main = new Main();`

                          List<Student> details = new ArrayList<Student>() ;
                          Student s1 = new Student();
                          Student s2 = new Student();
                          Student s3 = new Student();
                          s1.setId("ID1");
                          s1.setValue("1");
                          s2.setId("ID2");
                          s2.setValue("2");
                          s3.setId("ID3");
                          s3.setValue("3");                          
                         details.add(s1);
                         details.add(s2);
                         details.add(s3);

                         main.setDetails(details);
                         ary.put(details);

                   Employee emp = new Employee();
                   emp.setName("Mike");
                   emp.setSalary(1000);

                   XML xml = new XML();

                   xml.setDetails(ary.toString());
                   xml.setEmp(emp);
                   xml.setId("1"); 

                   return xml;
                   } 
                }


XML class (JAXB)

                package com.test1;

                import java.util.HashMap;
                import java.util.Map;
                import javax.xml.bind.annotation.XmlElement;
                import javax.xml.bind.annotation.XmlRootElement;

                @XmlRootElement
                public class XML {  

                String id;
                Employee emp;
                String details;

                public String getId() {
                   return id;
                }

                 public void setId(String id) {
                   this.id = id;
                 }

                 public Employee getEmp() {
                    return emp;
                }

                public void setEmp(Employee emp) {
                   this.emp = emp;
                }

                public String getDetails() {
                    return details;
                }

                public void setDetails(String details) {
                    this.details = details;
                }
                }

Student Class 
                package com.test1;

                public class Student {

                   String id;
                   String Value;
                   public String getId() {
                          return id;
                   }
                   public void setId(String id) {
                          this.id = id;
                   }
                   public String getValue() {
                          return Value;
                   }
                   public void setValue(String value) {
                          Value = value;

Main Class
                 package com.test1;

                import java.util.List;

                public class Main {



                   List<Student> details;

                   public List<Student> getDetails() {
                          return details;
                   }

                   public void setDetails(List<Student> details) {
                          this.details = details;
                   }


                }

So when i hit my service am getting response as

            {
              "details" : "[[{\"id\":\"ID1\",\"value\":\"1\"},{\"id\":\"ID2\",\"value\":\"2\"},{\"id\":\"ID3\",\"value\":\"3\"}]]",
              "emp" : {
                "name" : "Arun",
                "salary" : "1000.0"
              },
              "id" : "1"
            }

Expected String valuse as

    [{"id":"ID1","value":"1"},{"id":"ID2","value":"2"},{"id":"ID3","value":"3"}]

My Question is Why JSONArray which was converted as String contains "\\" on every key value pair. is there any way to overcome this?

Use Jackson or google-gson or any other library. For example http://howtodoinjava.com/2014/06/16/jackson-examples-convert-java-object-to-from-json/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM