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

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

3 回答 3

1

更新

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

我运行了以下代码:

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 是一个字符串,在任何流中都显示相同。

你有没有检查过你在某个地方没有错字? 我建议您在项目中完全复制并粘贴代码。

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

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

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

于 2013-06-14T18:22:38.237 回答
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();
}

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

于 2013-06-14T18:04:12.757 回答
0

这是原始代码吗?

你确定没有像这样的错字:

out.print(gson);

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

于 2013-06-14T18:10:11.807 回答