在JAXB中将JSON对象转换为字符串

时间:2015-01-02 09:17:57

标签: java json web-services

编写一个Webservice端点,它将产生JSON和XML响应。在这里,我想将一个对象转换为JSON并将其设置为字符串。当我尝试使用以下代码时

Webservice端点

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;
                   }


                }

所以,当我点击我的服务时,我的回复是

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

预期字符串值为

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

我的问题是为什么转换为String的JSONArray在每个键值对上都包含“\”。有什么方法可以克服这个问题吗?

1 个答案:

答案 0 :(得分:1)

使用Jackson或google-gson或任何其他库。 例如 http://howtodoinjava.com/2014/06/16/jackson-examples-convert-java-object-to-from-json/