2010-04-02 62 views
1

我目前正在尝试将生成的JSON字符串传递给dojo进行解析,并遇到一些问题。该servlet只是将它作为一个字符串,像这样:将json从servlet传递到dojo

response.getWriter().append("{ \"data\": {"); 
response.getWriter().append("\"type\": \"facing\","); 
response.getWriter().append("\"score\": " + "\"" + score + "\","); 
response.getWriter().append("\"count\":" + "\"" + count + "\""); 
response.getWriter().append("}}"); 

它打印为:

{"data":{"type":"facing","score":"10","count":"24"}} 

而且在道场最终解析看起来像这样:

dojo.xhrPost({ 
    url: url, 
    handleAs: "json", 
    load: function(data) { 
     alert(data); 
     /* Parse Not working */ 
     alert(data.data[0].type); 
    }, 
    error: function(error) { 
     alert("No dice") 
    } 
}); 

的主要问题是data.data [0] .type是什么都没有返回,但是当我把它打印出来作为文本JSON似乎被正确格式化。任何帮助,这将不胜感激。

回答

0

自解: 只能使用[]运算符从数组中取消引用值,但如果从对象中提取值,请使用点符号。 所以要获得类型只需要做:data.data.type

0

我强烈建议使用Google Gson将全部javabean的映射和/或集合转换为JSON,反之亦然。你可以找到here几个例子。学习JSON也会有很大帮助。