使用JsonDeserializer将ajax中的JSON对象解析为自定义对象

时间:2014-04-23 10:55:01

标签: java jquery ajax json gson

我使用gson将Test ofTerail Class的对象序列化为Json。

TestDetail类 -


public class TestDetail implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="test_details_id")
    private int testDetailsId;

    @Column(name="test_exists_cc")
    private byte testExistsCc;

    @Column(name="test_name")
    private String testName;

    @Column(name="test_package")
    private String testPackage;

     @Column(name="test_automated_on")
      private Date testAutomationDate;

      //bi-directional many-to-one association to VariableDetail
      @OneToMany(mappedBy="testDetail", fetch=FetchType.EAGER)
      private Set<VariableDetail> variableDetails;

我的Servlet代码是---

      String productSelected = request.getParameter("productSelected");


ProductDetailDAO productDetailDAO = new ProductDetailDAOImpl();
  ProductDetail productDetail =  productDetailDAO.getProductByName(productSelected);

  Set<TestDetail> testDetailSet = productDetail.getTestDetails();

  productDetailDAO.closeEntityManager();

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(TestDetail.class, new TestDetailJsonSerializer());
Gson gson = builder.create();

String json = gson.toJson(testDetailSet);
//gson.fromJson(json, TestDetail.class);
System.out.println(json);

  response.getWriter().write(json);

在consol中打印json输出时我得到了这个 -


["com.amadeus.automatepckg.db.entities.TestDetail@18e4593","com.amadeus.automatepckg.db.entities.TestDetail@38a3ca","com.amadeus.automatepckg.db.entities.TestDetail@a6bcec","com.amadeus.automatepckg.db.entities.TestDetail@115a56d","com.amadeus.automatepckg.db.entities.TestDetail@89583a","com.amadeus.automatepckg.db.entities.TestDetail@6aa01a","com.amadeus.automatepckg.db.entities.TestDetail@bd5fa1","com.amadeus.automatepckg.db.entities.TestDetail@fe8ee7","com.amadeus.automatepckg.db.entities.TestDetail@7d6ca2","com.amadeus.automatepckg.db.entities.TestDetail@30c6ca","com.amadeus.automatepckg.db.entities.TestDetail@4b0a55","com.amadeus.automatepckg.db.entities.TestDetail@e89bed","com.amadeus.automatepckg.db.entities.TestDetail@10924b1","com.amadeus.automatepckg.db.entities.TestDetail@12a15d3","com.amadeus.automatepckg.db.entities.TestDetail@12c1685","com.amadeus.automatepckg.db.entities.TestDetail@1281b8c","com.amadeus.automatepckg.db.entities.TestDetail@939339","com.amadeus.automatepckg.db.entities.TestDetail@15343c2","com.amadeus.automatepckg.db.entities.TestDetail@75324a","com.amadeus.automatepckg.db.entities.TestDetail@1b9fc6","com.amadeus.automatepckg.db.entities.TestDetail@19699dd","com.amadeus.automatepckg.db.entities.TestDetail@147f26f","com.amadeus.automatepckg.db.entities.TestDetail@1f28508","com.amadeus.automatepckg.db.entities.TestDetail@a5de12","com.amadeus.automatepckg.db.entities.TestDetail@d5cfd6","com.amadeus.automatepckg.db.entities.TestDetail@18587ac","com.amadeus.automatepckg.db.entities.TestDetail@7c0a2c","com.amadeus.automatepckg.db.entities.TestDetail@16d9fe1","com.amadeus.automatepckg.db.entities.TestDetail@17fd935","com.amadeus.automatepckg.db.entities.TestDetail@6d6c90","com.amadeus.automatepckg.db.entities.TestDetail@17a765c","com.amadeus.automatepckg.db.entities.TestDetail@8d5581","com.amadeus.automatepckg.db.entities.TestDetail@102ab48","com.amadeus.automatepckg.db.entities.TestDetail@1402894","com.amadeus.automatepckg.db.entities.TestDetail@179eb02","com.amadeus.automatepckg.db.entities.TestDetail@35449e","com.amadeus.automatepckg.db.entities.TestDetail@8bed1c","com.amadeus.automatepckg.db.entities.TestDetail@1b72911","com.amadeus.automatepckg.db.entities.TestDetail@196a464","com.amadeus.automatepckg.db.entities.TestDetail@28b522","com.amadeus.automatepckg.db.entities.TestDetail@4dbd0f","com.amadeus.automatepckg.db.entities.TestDetail@ff7853","com.amadeus.automatepckg.db.entities.TestDetail@2ff65","com.amadeus.automatepckg.db.entities.TestDetail@6f03ec","com.amadeus.automatepckg.db.entities.TestDetail@976b5b","com.amadeus.automatepckg.db.entities.TestDetail@3cee0d","com.amadeus.automatepckg.db.entities.TestDetail@d150be","com.amadeus.automatepckg.db.entities.TestDetail@154e92c","com.amadeus.automatepckg.db.entities.TestDetail@bed71","com.amadeus.automatepckg.db.entities.TestDetail@fc0795","com.amadeus.automatepckg.db.entities.TestDetail@f0ce45","com.amadeus.automatepckg.db.entities.TestDetail@111a20c","com.amadeus.automatepckg.db.entities.TestDetail@b1a201","com.amadeus.automatepckg.db.entities.TestDetail@176323e","com.amadeus.automatepckg.db.entities.TestDetail@1987899","com.amadeus.automatepckg.db.entities.TestDetail@4f96a0","com.amadeus.automatepckg.db.entities.TestDetail@1fb14e","com.amadeus.automatepckg.db.entities.TestDetail@dc907a","com.amadeus.automatepckg.db.entities.TestDetail@6727f0","com.amadeus.automatepckg.db.entities.TestDetail@57e44e","com.amadeus.automatepckg.db.entities.TestDetail@91630f","com.amadeus.automatepckg.db.entities.TestDetail@cd504e","com.amadeus.automatepckg.db.entities.TestDetail@3b0790","com.amadeus.automatepckg.db.entities.TestDetail@1fee2b4","com.amadeus.automatepckg.db.entities.TestDetail@1e95777","com.amadeus.automatepckg.db.entities.TestDetail@3496c9","com.amadeus.automatepckg.db.entities.TestDetail@560854"]

自定义序列化程序 -


   import java.lang.reflect.Type;

import com.amadeus.automatepckg.db.entities.TestDetail;
import com.google.gson.*;

public class TestDetailJsonSerializer implements JsonSerializer<TestDetail> {
    @Override
    public JsonElement serialize(TestDetail entity, Type typeOfSrc, JsonSerializationContext context) {
       return  new JsonPrimitive(entity.toString());
    }

}

自定义DeSerializer - TestDetailJsonSerializer


   import java.lang.reflect.Type;
import com.amadeus.automatepckg.db.entities.TestDetail;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class TestDetailJonsDeSerializer implements JsonDeserializer<TestDetail> {
  @Override
  public TestDetail deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
  throws JsonParseException {
    return new TestDetail();
  }
}

jsp中的ajax函数


$.ajax({
         url: 'GetTestDetailsForProduct',
         data: 'productSelected='+$("#productSelectBox").val(),
         type: 'get',
         dataType:'json',
          success: function(result){
              $(result).each(function(index,element){
                  alert(element); // showing object address same as console output
                      alert(element.testPackage); // showing undefined in alert
              });
        //    $('#availableTests').val(jsonTestDetails);
          }

现在我想从ajax响应中访问TestDetail的成员变量,为此我如何将JSON对象解析回TestDetail对象。

1 个答案:

答案 0 :(得分:0)

替换

return  new JsonPrimitive(entity.toString());

return  new JsonPrimitive(entity);

更新

你是对的,它是一个原始的表达,它不能成为对象。

但是你的代码仍然使用实体对象的String表示来构建json对象,而defaul就像&com; package.Class@18e4593'。这显然是无稽之谈,你需要提供Object到json格式化程序而不是它的toString()结果。

为什么不使用gson.toJson(obj)?