代码之家  ›  专栏  ›  技术社区  ›  Rachel

如何在dojo中读取JSONObject,JSONObject是在JSP中创建的?

  •  0
  • Rachel  · 技术社区  · 14 年前

    我的jsp中有一个JSONObject。

    JSONObject  myJSONObj = new JSONObject();
    

    myJSONObj 在我的dojo或*.js文件中,在 'onload function' .

    2 回复  |  直到 14 年前
        1
  •  0
  •   jebberwocky    14 年前

    可能想试试这个:

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

    getJSON是一个函数,它将myJSONObj返回为JSON格式的字符串

    更新: 简短的例子。希望有帮助

    <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>
    
        2
  •  0
  •   subhaze    14 年前

    尝试将输出包装为 toJson

    在DOM准备好时运行它 addOnLoad

    this

    [编辑]

    我突然意识到我应该把“toJson”改成 fromJson dojo: json string to json object