Tomcat servlet与json对象行为奇怪

时间:2013-06-14 17:55:52

标签: json java-ee tomcat servlets gson

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    Gson gson = new Gson();
    LocationTypes locTypes = new LocationTypes();
    String json = gson.toJson(locTypes);

    out.print(json);
    out.flush();
}

如果我使用上面的代码和System.out.println(json),它看起来像这样:

    {"locationTypes":["Hospital","Church","Restaurant","Bar","Other"]}

我在浏览器中得到的内容,当指向servlet的url时,我得到了这个:

    {"calls":{"threadLocalHashCode":-2084311414},"typeTokenCache":{"com.google.gson.InstanceCreator\u003c?\u003e":{},"int":{},"java.lang.String":{},"java.lang.String[]":{},"java.util.Map\u003ccom.google.gson.reflect.TypeToken\u003c?\u003e, com.google.gson.TypeAdapter\u003c?\u003e\u003e":{},"java.util.List\u003ccom.google.gson.TypeAdapterFactory\u003e":{},"java.lang.ThreadLocal\u003cjava.util.Map\u003ccom.google.gson.reflect.TypeToken\u003c?\u003e, com.google.gson.Gson$FutureTypeAdapter\u003c?\u003e\u003e\u003e":{},"com.google.gson.TypeAdapterFactory":{},"com.google.gson.JsonDeserializationContext":{},"com.google.gson.reflect.TypeToken\u003c?\u003e":{},"java.util.Map\u003cjava.lang.reflect.Type, com.google.gson.InstanceCreator\u003c?\u003e\u003e":{},"com.google.gson.Gson":{},"boolean":{},"java.lang.reflect.Type":{},"data.LocationTypes":{},"java.lang.Class\u003c? super ?\u003e":{},"java.lang.Integer":{},"com.google.gson.internal.ConstructorConstructor":{},"com.google.gson.TypeAdapter\u003c?\u003e":{},"com.google.gson.JsonSerializationContext":{}},"factories":[null,null,{"version":-1.0,"modifiers":136,"serializeInnerClasses":true,"requireExpose":false,"serializationStrategies":[],"deserializationStrategies":[]},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"constructorConstructor":{"instanceCreators":{}}},{"constructorConstructor":{"instanceCreators":{}},"complexMapKeySerialization":false},{"constructorConstructor":{"instanceCreators":{}},"fieldNamingPolicy":"IDENTITY","excluder":{"version":-1.0,"modifiers":136,"serializeInnerClasses":true,"requireExpose":false,"serializationStrategies":[],"deserializationStrategies":[]}}],"constructorConstructor":{"instanceCreators":{}},"serializeNulls":false,"htmlSafe":true,"generateNonExecutableJson":false,"prettyPrinting":false}

3 个答案:

答案 0 :(得分:1)

更新

我已经复制了你的错误。 不幸的是,您正在传递要转换为JSON的gson对象。 你的问题是错字/错误的结果。

我运行了以下代码:

public static void main (String args[])
    {   
          Gson gson = new Gson();
          String json = gson.toJson(gson);
          System.out.println(json);
    }

并收到以下内容:

{"calls":{"threadLocalHashCode":1253254570},"typeTokenCache":{"com.google.gson.Gson":{},"com.google.gson.reflect.TypeToken\u003c?\u003e":{},"com.google.gson.internal.ConstructorConstructor":{},"com.google.gson.InstanceCreator\u003c?\u003e":{},"java.lang.reflect.Type":{},"boolean":{},"int":{},"com.google.gson.JsonDeserializationContext":{},"com.google.gson.JsonSerializationContext":{},"java.lang.ThreadLocal\u003cjava.util.Map\u003ccom.google.gson.reflect.TypeToken\u003c?\u003e, com.google.gson.Gson$FutureTypeAdapter\u003c?\u003e\u003e\u003e":{},"java.util.List\u003ccom.google.gson.TypeAdapterFactory\u003e":{},"java.util.Map\u003cjava.lang.reflect.Type, com.google.gson.InstanceCreator\u003c?\u003e\u003e":{},"com.google.gson.TypeAdapter\u003c?\u003e":{},"java.lang.Integer":{},"com.google.gson.TypeAdapterFactory":{},"java.lang.Class\u003c? super ?\u003e":{},"java.util.Map\u003ccom.google.gson.reflect.TypeToken\u003c?\u003e, com.google.gson.TypeAdapter\u003c?\u003e\u003e":{}},"factories":[null,null,{"version":-1.0,"modifiers":136,"serializeInnerClasses":true,"requireExpose":false,"serializationStrategies":[],"deserializationStrategies":[]},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"constructorConstructor":{"instanceCreators":{}}},{"constructorConstructor":{"instanceCreators":{}},"complexMapKeySerialization":false},{"constructorConstructor":{"instanceCreators":{}},"fieldNamingPolicy":"IDENTITY","excluder":{"version":-1.0,"modifiers":136,"serializeInnerClasses":true,"requireExpose":false,"serializationStrategies":[],"deserializationStrategies":[]}}],"constructorConstructor":{"instanceCreators":{}},"serializeNulls":false,"htmlSafe":true,"generateNonExecutableJson":false,"prettyPrinting":false}

向Pragmateek致敬,同时检查GSON SVN回购。

原始答案

System.out.println(json);实际上不可能给你一个不同于

的结果
out.print(json);
out.flush();

json是一个String,在任何流中都是相同的。

您是否检查过某个地方没有拼写错误? 我建议您完全按照项目中的方式复制和粘贴代码。

在浏览器中,您将获得一个JSON版本的对象,该对象已将所有对象的值/字段序列化为JSON。

生成的JSON中的许多键是您尝试序列化为JSON的对象的实际字段,如Pragmateek所说。

几乎可以怀疑你正在传递你的GSON对象以转换为JSON ....

答案 1 :(得分:0)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    Gson gson = new Gson();
    LocationTypes locTypes = new LocationTypes();
    String json = gson.toJson(locTypes);
    response.setContentType("application/json");
    out.print(json);
    out.flush();
}

尝试设置如上所述的内容类型

答案 2 :(得分:0)

这是原始代码吗?

你确定没有像这样的拼写错误:

out.print(gson);

因为奇怪的JSON看起来真的像序列化的GSON库对象......