无法在javascript中访问java变量

时间:2015-10-21 11:26:26

标签: javascript java json jsp

我正在尝试访问一个在javascript中保存Json数据的java变量,但是我没有获取数据,而是获得了以下html源代码。

的java:

 Gson gson = new Gson();
 String json = gson.toJson(obj);
 request.setAttribute("gsonData", gson);

的javascript:

var dataset = ${gsonData};

html页面源中的数据:

var dataset = {serializeNulls:falsefactories:[Factory[type=com.google.gson.JsonElement,
adapter=com.google.gson.internal.bind.TypeAdapters$25@719474],
com.google.gson.internal.bind.ObjectTypeAdapter$1@31d22,
 Factory[type=java.lang.String,
adapter=com.google.gson.internal.bind.TypeAdapters$13@1aacfcf],
 Factory[type=java.lang.Integer+int,
adapter=com.google.gson.internal.bind.TypeAdapters$7@977b2d],
 Factory[type=java.lang.Boolean+boolean,
adapter=com.google.gson.internal.bind.TypeAdapters$3@b18cbf],
 Factory[type=java.lang.Byte+byte,
adapter=com.google.gson.internal.bind.TypeAdapters$5@7964c6],
 Factory[type=java.lang.Short+short,
adapter=com.google.gson.internal.bind.TypeAdapters$6@1ec0cb1],
 Factory[type=java.lang.Long+long,
adapter=com.google.gson.internal.bind.TypeAdapters$8@9752b2],
 Factory[type=java.lang.Double+double,
adapter=com.google.gson.Gson$4@992d41],
 Factory[type=java.lang.Float+float,
adapter=com.google.gson.Gson$5@fedab9],
 Factory[type=java.lang.Number,
adapter=com.google.gson.internal.bind.TypeAdapters$11@89e335],
 Factory[type=java.lang.Character+char,
adapter=com.google.gson.internal.bind.TypeAdapters$12@1d2554],
 Factory[type=java.lang.StringBuilder,
adapter=com.google.gson.internal.bind.TypeAdapters$16@16294d3],
 Factory[type=java.lang.StringBuffer,
adapter=com.google.gson.internal.bind.TypeAdapters$17@1a4510f],
 Factory[type=java.math.BigDecimal,
adapter=com.google.gson.internal.bind.TypeAdapters$14@4b80c5],
 Factory[type=java.math.BigInteger,
adapter=com.google.gson.internal.bind.TypeAdapters$15@133375d],
 Factory[type=java.net.URL,
adapter=com.google.gson.internal.bind.TypeAdapters$18@30f324],
 Factory[type=java.net.URI,
adapter=com.google.gson.internal.bind.TypeAdapters$19@16be244],
 Factory[type=java.util.UUID,
adapter=com.google.gson.internal.bind.TypeAdapters$21@1501d84],
 Factory[type=java.util.Locale,
adapter=com.google.gson.internal.bind.TypeAdapters$24@1255f6c],
 Factory[typeHierarchy=java.net.InetAddress,
adapter=com.google.gson.internal.bind.TypeAdapters$20@a92e7f],
 Factory[type=java.util.BitSet,
adapter=com.google.gson.internal.bind.TypeAdapters$2@5a9aac],
 com.google.gson.internal.bind.DateTypeAdapter$1@1c5051e,
 Factory[type=java.util.Calendar+java.util.GregorianCalendar,
adapter=com.google.gson.internal.bind.TypeAdapters$23@6efe35],
 com.google.gson.internal.bind.TimeTypeAdapter$1@187e651,
 com.google.gson.internal.bind.SqlDateTypeAdapter$1@12c81b7,
 com.google.gson.internal.bind.TypeAdapters$22@3a8f79,
 com.google.gson.internal.bind.ArrayTypeAdapter$1@1744f5d,
 com.google.gson.internal.bind.TypeAdapters$26@16b870c,
 Factory[type=java.lang.Class,
adapter=com.google.gson.internal.bind.TypeAdapters$1@9f99af],
 com.google.gson.internal.Excluder@408302,
 com.google.gson.internal.bind.CollectionTypeAdapterFactory@c22e44,
 com.google.gson.internal.bind.MapTypeAdapterFactory@446bcc,
 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory@1d8ae60],
instanceCreators:{}};

访问数据的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

您写道:

request.setAttribute("gsonData", gson);

但是你不想要更多:

request.setAttribute("jsonData", json);

即。放置obj而不是GSon对象的内容?

答案 1 :(得分:0)

试试:

var dataset = JSON.decode(gsonData);
dataset['serializeNulls:falsefactories'].Factory;