开发者

How to read JSONObject in dojo, where JSONObject is created in JSP?

How to read JSONObject in dojo?

I h开发者_开发知识库ave a JSONObject in my jsp.

JSONObject  myJSONObj = new JSONObject();

How can I read this myJSONObj in my dojo or *.js file, in 'onload function'.


might want to try this:

window.load = function(){
 var myjson = <%= getJSON() %>
 //using myjson
}

getJSON is a function which returns myJSONObj as JSON formatted String

update: short example. hope it helps

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%
class Foor{
    public String getJSON(){
        return "{foo:'bar'}";
    }
}

Foor myfoo = new Foor();

%>
<script language=javascript>
var myjson = <%= myfoo.getJSON()%>
alert(myjson.foo);
</script>
</head>
<body>

</body>
</html>


Try wrapping the output in the toJson function.

For running it when the DOM is ready try addOnLoad

And if you're wanting to load it via AJAX onLoad try this

[EDIT]

It just dawned on me that I put "toJson" it should be fromJson since you're needing to convert a JSON string to a JSON object. Here is an example dojo: json string to json object

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜