How to read a JSON object already preload withing a js file in the HTML Header

107 views
Skip to first unread message

Iglesu

unread,
Nov 13, 2007, 5:16:21 PM11/13/07
to Google Web Toolkit
Hi,

I have a need to load some static data in JSON format that initializes
some of the widgets in the screen.
It would advantageous to me if that static data where already loaded
and ready to be used before the gwt logic kicks in.
Alternatively I could use async http requests to pick it up but it's
not very graceful.

The JSON static data is made mostly of JSON arrays (strings and
numbers). I tried JSNI but couldn't find a easy way to load those
simple JSON objects into the GWT.

I wonder if someone have come across that and have an easy way to do
that.

Thanks in advance!

Milan Jaric

unread,
Nov 13, 2007, 6:14:44 PM11/13/07
to Google-We...@googlegroups.com
GWT is compiler! so when you wrote java code you use gwt compiler to produce js files witch are static things, or  things that are not your business objects, because your BO are pure java and  need to be  instantiate at run time of servlet with data from database. So my guess is that you can add another script tag in your page witch can ref. to RPC service with initial parameters before gwt.js script include, then data will be loaded into page before yourEntryPoint. But  I think it is beter to  show  splash  to user, then do this hack. Think like this. You have loaded your entry point in other page ( reusability ) ... where is my data??? well i left it on the oder page :(

Iglesu

unread,
Nov 13, 2007, 6:32:17 PM11/13/07
to Google Web Toolkit
Here is an example of what I'm trying to achieve.

example.js
//<![CDATA[
var my_array_of_elements=["elem1","elem2","elem3"];
//]]>

myHtml.html
<html>
<head>
<script language='javascript' src='example.js'></script>
<script src="gwt...js"></script>
</head>
...
</hmtl>

and my question is
how do it get the "my_array_of_elements" into, lets say, a JSONArray?
is there a simple way to do that?

Thanks!

Milan Jaric

unread,
Nov 13, 2007, 6:44:59 PM11/13/07
to Google-We...@googlegroups.com
Json is short of Java Script Object Notation

so when you declare an object with anonymous constructor in javascript you do this
vara anObject = {
      property1 : "property one is text",
      property2 : new function(param){ return param }
}

so JSON will look like this

var JSONObject = {"objects": [ // <---- start of array
        {"property1": "PRIVMSG", "property2": "newURI" }, // <-- this is object1
        {"property1": "PRIVMSG", "property2": "deleteURI" }, // <-- this is object2
        {"property1": "PRIVMSG", "property2": "randomURI", } // etc...
    ]
};

and gwt support for this is http://code.google.com/p/rocket-gwt/wiki/JsonSerialization
--
Milan Jaric

Iglesu

unread,
Nov 13, 2007, 7:23:35 PM11/13/07
to Google Web Toolkit
thank you for your reply.
In my previous email I meant either JSON or native javascript ... I
don't care as long as I can have it in the java side as an object.

Regarding the rocket-gwt link .. I don't get it ...
I copy below an extract from that documentation

>>
"Include required import statements.

import rocket.json.client.JsonSerializer;

To de serialize a json object to java a reference a JsonSerializer
must obtained using deferred binding.

JSONObject jsonObject = // already got it from somewhere...
JsonSerializer serializer = (JsonSerializer)
GWT.create( SomeClass.class );

>>

So it says the JSONObject is already gotten from somewhere ... well
that's precisely what I like to know. How do I get it there?

Thanks

On Nov 13, 4:44 pm, "Milan Jaric" <milan.ja...@gmail.com> wrote:
> Json is short of Java Script Object Notation
>
> so when you declare an object with anonymous constructor in javascript you
> do this
> vara anObject = {
> property1 : "property one is text",
> property2 : new function(param){ return param }
>
> }
>
> so JSON will look like this
>
> var JSONObject = {"objects": [ // <---- start of array
> {"property1": "PRIVMSG", "property2": "newURI" }, // <-- this is
> object1
> {"property1": "PRIVMSG", "property2": "deleteURI" }, // <-- this is
> object2
> {"property1": "PRIVMSG", "property2": "randomURI", } // etc...
> ]
>
> };
>

> and gwt support for this ishttp://code.google.com/p/rocket-gwt/wiki/JsonSerialization

Iglesu

unread,
Nov 13, 2007, 7:59:20 PM11/13/07
to Google Web Toolkit
Actually some reading I found that the solution to this problem is
quite simple by using JSNI and the JavaScriptObject

e.g.
private native JavaScriptObject readJSObject () /*-{
return $wnd.my_array_of_elements;
}-*/;

public void foo () {
JavaScriptObject js= null;
JSONArray ja=null;
js = readJSObject();
ja=new JSONObject(js);
}
So now "ja" will have the array as a JSONArray object in GWT.

if the JavaScript were a more complex JSON structure such us
var my_json_object = { key1:[1,2,3,4];key2:"hello";key3:{morekeys:
["a","b"];}}

then in the foo() method instead of having a JSONArray simply have a
JSONObject

Peter Blazejewicz

unread,
Nov 13, 2007, 8:22:34 PM11/13/07
to Google Web Toolkit
hi Iglesu,
similiar topic on the very same subject (with different solutiosn
discussed):
http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/93d81e01075debae#

regards,
Peter

Iglesu

unread,
Nov 15, 2007, 5:11:41 PM11/15/07
to Google Web Toolkit
Thanks for the link!
That kind of binding would be ideal ... except that I think its not as
flexible as I'd need it to be.
E.g.
Let's say I have a JSON object such us:
some_key: { key1:[1,2,3], ... , keyN:[4,7,89]}

where key1 through keyN is a variable number.
If I understood well the JSIO I would need to specify a N getKey
methods?? or is it a way to get the keys of the hash ?


On Nov 13, 6:22 pm, Peter Blazejewicz <peter.blazejew...@gmail.com>
wrote:
> hi Iglesu,
> similiar topic on the very same subject (with different solutiosn
> discussed):http://groups.google.com/group/Google-Web-Toolkit/browse_thread/threa...

Peter Blazejewicz

unread,
Nov 15, 2007, 6:05:56 PM11/15/07
to Google Web Toolkit
hi,

isn't is map like collection? you could try with JSList,

regards,
peter
Reply all
Reply to author
Forward
0 new messages