how to load json from a servlet into hypertree.js dynamically

895 views
Skip to first unread message

kishan

unread,
Feb 1, 2012, 2:58:44 AM2/1/12
to JavaScript InfoVis Toolkit
hi all,

im new to the infovis toolkit and need some help,


i have collected few information from a specific website, details like
name,age,location etc and converted them into json object using gson
in my servlet
now can any one help me out in how to load this json into the
hypertree.js dynamically with out writting it to a file


thanks in advance

kishan

unread,
Feb 1, 2012, 3:17:19 AM2/1/12
to JavaScript InfoVis Toolkit

ie how to call json from servlet/jsp into the hypertree.js >>> init
function !!!!
dynamically ...

Zameer Ansari

unread,
Feb 1, 2012, 3:26:25 AM2/1/12
to javascript-information...@googlegroups.com
call your servelet with ajax on page load event.
   jQuery(document).ready(function() { 
        
        jQuery.ajax({ 
                        url  : "yourservlet.java", 
                        type : "POST", 
                        async: false, 
                        data : "param=value",
                        success: function(data) { 
                        //alert("data" + data); 
                        var obj = jQuery.parseJSON(data); 
                        init(obj); //this line will call init function with JSON loaded after this call
                        } 
                });      
        }); 
-Zameer


--
You received this message because you are subscribed to the Google Groups "JavaScript InfoVis Toolkit" group.
To post to this group, send email to javascript-information...@googlegroups.com.
To unsubscribe from this group, send email to javascript-information-visua...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javascript-information-visualization-toolkit?hl=en.


kishan

unread,
Feb 2, 2012, 3:51:25 AM2/2/12
to JavaScript InfoVis Toolkit
hi zameer,

i tried to make a ajax call to my servlet with the above code but i
get a undefined msg in my alert at alert("data" + data);
can you pls provide example prog on how to make ajax calls and load
json onto hypertree.js dynamically..

thanks in advance
kishan
> >http://groups.google.com/group/javascript-information-visualization-t...
> > .

Brian

unread,
Feb 3, 2012, 1:44:31 PM2/3/12
to JavaScript InfoVis Toolkit
I too am having a similar issue with loading external data (json) into
a space tree. Can anyone shed any light on how to do it successfully.
A tutorial on how to do this with jQuery would be Briliant!

Regards

vprasad

unread,
Feb 3, 2012, 9:31:20 PM2/3/12
to JavaScript InfoVis Toolkit
going off of what Zameer replied,

in the init() method, you use HT.loadJSON(obj) instead of the json
string you have in the example.js. so it would be something like:

init(obj){
// Existing code..

change ht.loadJSON(json);
to ht.loadJSON(obj);

Jim Klo

unread,
Feb 3, 2012, 10:16:22 PM2/3/12
to javascript-information...@googlegroups.com, JavaScript InfoVis Toolkit
Do you have any cross domain issues? If so you need to use JSONP or add the right OPTIONS response to your servlet.

Sent from my iPhone

> For more options, visit this group at http://groups.google.com/group/javascript-information-visualization-toolkit?hl=en.
>

kishan

unread,
Feb 9, 2012, 2:01:46 AM2/9/12
to JavaScript InfoVis Toolkit
hi all,

i been tryin to call json from a servlet into javascript using jquery
and here is my snippet

jQuery(document).ready(function() {
alert("inside ready method");
jQuery.ajax({

url : "http://localhost:_________",
type : "POST",
async : false,
dataType : 'json',
success : function(data) {
alert('Success!');
var obj = jQuery.parseJSON(json);
},

});
});



i get a success alert and my servlet is been call'd from the above
jquery but json object is not transferred from servlet to javascript
can anyone pls explain where im doing it wrong...

thanks in advance

Zameer Ansari

unread,
Feb 9, 2012, 2:08:42 AM2/9/12
to javascript-information...@googlegroups.com
3 points:
1. Is your servlet generating JSON, if you call it in a separate browser.
2. You forgot to call 'init' method on success event.
3. change init method (example.js or example2.js). function init(json){....} 


-Zameer

On Thu, Feb 9, 2012 at 10:01 AM, kishan <meetin...@gmail.com> wrote:
hi all,

i been tryin to call json from a servlet into javascript using jquery
and here is my snippet

jQuery(document).ready(function() {
               alert("inside ready method");
               jQuery.ajax({

                       url : "http://localhost:_________",
                       type : "POST",
                       async : false,
                       dataType : 'json',
                        success : function(data) {
                               alert('Success!');
                               var obj = jQuery.parseJSON(json);
                                   init(obj); 
                       },

               });
       });



i get a success alert and my servlet is been call'd from the above
jquery but json object is not transferred from servlet to javascript
can anyone pls explain where im doing it wrong...

thanks in advance

--
You received this message because you are subscribed to the Google Groups "JavaScript InfoVis Toolkit" group.
To post to this group, send email to javascript-information...@googlegroups.com.
To unsubscribe from this group, send email to javascript-information-visua...@googlegroups.com.

kishan

unread,
Feb 9, 2012, 2:16:52 AM2/9/12
to JavaScript InfoVis Toolkit
hi zameer !!

yes i can generate json separately in a browser
i have also used init method as shown below

success: function(data) {
alert("data" + data);
var obj = jQuery.parseJSON(data);
init(obj);


but @ alert("data" + data); i get datanull !!!!
> >http://groups.google.com/group/javascript-information-visualization-t...
> > .

Zameer Ansari

unread,
Feb 9, 2012, 2:21:57 AM2/9/12
to javascript-information...@googlegroups.com
my code is running without this "dataType : 'json' ". Try it but I'm not sure it can fix the problem.

-Zameer

kishan

unread,
Feb 9, 2012, 2:30:27 AM2/9/12
to JavaScript InfoVis Toolkit
hi zameer tried tat


var obj = jQuery.parseJSON(data);
document.write(obj);

still getting null on the above code !!!


is there anything else to be written on my servlet side code other
than jst creating a json object and setting
response.setContentType("text/javascript");
response.setCharacterEncoding("UTF-8"); ??



On Feb 9, 12:21 pm, Zameer Ansari <zameerans...@gmail.com> wrote:
> my code is running without this "dataType : 'json' ". Try it but I'm not
> sure it can fix the problem.
>
> -Zameer
>
> >http://groups.google.com/group/javascript-information-visualization-t...
> > .

Jim Klo

unread,
Feb 9, 2012, 10:08:07 AM2/9/12
to javascript-information...@googlegroups.com
I'd asked this previously, as the symptom you describe sounds like a cross origin problem, where the domain name of the web page is not the same as your servlet. Even different ports create a cross origin issue. And using a file:///path/to/page.html reference to you jit page and http://localhost/myservlet violates this too. If you use firebug and you see an OPTIONS request being made when a GET request should be made, this is certainly the problem. If it's making the GET request, is it making it right? And is anything being returned - it will show in firebug.

If not that, can you use curl to call your servlet and paste the output into jsonlint.com and it validate?

Personally, I've had a lot of issues with $.getJSON() as my experience has seen issues with IE in the past. Instead try $.ajax().

Technically the mime-type should be application/json but I don't think all browsers like that and don't think jQuery cares AFAIK.

- Jim

> For more options, visit this group at http://groups.google.com/group/javascript-information-visualization-toolkit?hl=en.
>

kishan

unread,
Feb 9, 2012, 10:35:06 PM2/9/12
to JavaScript InfoVis Toolkit
hi jim,


i tried using

$.getJSON("http://localhost_________",
{
format: "json"
},
function(data) {

var obj= jQuery.parseJSON(data);
alert(obj);


});

when i run on firebug im able to see the json object in the console of
firebug as GET response and also shows the json objects but when i try
to parse it and make a alert as

alert(obj); // i get null
and when i make a alert as

alert(data); // i get [object Object]

what does this mean ?? is my json not parsed properly into javascript
object???



-kishan


On Feb 9, 8:08 pm, Jim Klo <jim....@gmail.com> wrote:
> I'd asked this previously, as the symptom you describe sounds like a cross origin problem, where the domain name of the web page is not the same as yourservlet. Even different ports create a cross origin issue. Andusingafile:///path/to/page.htmlreference to you jit page andhttp://localhost/myservletviolates this too. If you use firebug and you see an OPTIONS request being made when aGETrequest should be made, this is certainly the problem. If it's making theGETrequest, is it making it right? And is anything being returned - it will show in firebug.
>
> If not that, can you use curl to call yourservletand paste the outputintojsonlint.com and it validate?
>
> Personally, I've had a lot of issues with $.getJSON() as my experience has seen issues with IE in the past. Instead try $.ajax().
>
> Technically the mime-type should be application/jsonbut I don't think all browsers like that and don't thinkjQuerycares AFAIK.
>
> - Jim
>
> On Feb 8, 2012, at 11:30 PM, kishan <meetingkis...@gmail.com> wrote:
>
>
>
>
>
>
>
> > hi zameer tried tat
>
> > var obj =jQuery.parseJSON(data);
> >                document.write(obj);
>
> > still getting null on the above code !!!
>
> > is there anything else to be written on myservletside code other
> > than jst creating ajsonobject and setting
> > response.setContentType("text/javascript");
> > response.setCharacterEncoding("UTF-8"); ??
>
> > On Feb 9, 12:21 pm, Zameer Ansari <zameerans...@gmail.com> wrote:
> >> my code is running without this "dataType : 'json' ". Try it but I'm not
> >> sure it can fix the problem.
>
> >> -Zameer
>
> >> On Thu, Feb 9, 2012 at 10:16 AM, kishan <meetingkis...@gmail.com> wrote:
> >>> hi zameer !!
>
> >>> yes i can generatejsonseparately in a browser
> >>> i have also used init method as shown below
>
> >>> success: function(data) {
> >>>                 alert("data" + data);
> >>>                var obj =jQuery.parseJSON(data);
> >>>                init(obj);
>
> >>> but @ alert("data" + data); iget datanull !!!!
>
> >>> On Feb 9, 12:08 pm, Zameer Ansari <zameerans...@gmail.com> wrote:
> >>>> 3 points:
> >>>> 1. Is yourservletgeneratingJSON, if you call it in a separate browser.
> >>>> 2. You forgot to call 'init' method on success event.
> >>>> 3. change init method (example.js or example2.js). function init(json
> >>>> ){....}
>
> >>>> -Zameer
>
> >>>> On Thu, Feb 9, 2012 at 10:01 AM, kishan <meetingkis...@gmail.com> wrote:
> >>>>> hi all,
>
> >>>>> i been tryin to calljsonfrom aservletintojavascriptusingjquery
> >>>>> and here is my snippet
>
> >>>>>jQuery(document).ready(function() {
> >>>>>                alert("inside ready method");
> >>>>>                jQuery.ajax({
>
> >>>>>                        url : "http://localhost:_________",
> >>>>>                         type : "POST",
> >>>>>                        async : false,
> >>>>>                         dataType : 'json',
> >>>>>                         success : function(data) {
> >>>>>                                alert('Success!');
> >>>>>                                var obj =jQuery.parseJSON(json);
>
> >>>>                                    init(obj);
>
> >>>>>                        },
>
> >>>>>                });
> >>>>>        });
>
> >>>>> igeta success alert and myservletis been call'd from the above
> >>>>>jquerybutjsonobject is not transferred fromservlettojavascript
> >>>>> can anyone pls explain where im doing it wrong...
>
> >>>>> thanks in advance
>
> >>>>> --
> >>>>> You received this message because you are subscribed to the Google
> >>> Groups
> >>>>> "JavaScriptInfoVis Toolkit" group.
> >>>>> To post to this group, send email to
> >>>>>javascript-information...@googlegroups.com.
> >>>>> To unsubscribe from this group, send email to
>
> >>>javascript-information-visua...@googlegroups.com.
> >>>>> For more options, visit this group at
> >>>>>http://groups.google.com/group/javascript-information-visualization-t.
> >>> ..
> >>>>> .
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "JavaScriptInfoVis Toolkit" group.
> >>> To post to this group, send email to
> >>>javascript-information...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>>javascript-information-visua...@googlegroups.com.
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/javascript-information-visualization-t...
> >>> .
>
> > --
> > You received this message because you are subscribed to the Google Groups "JavaScriptInfoVis Toolkit" group.
> > To post to this group, send email tojavascript-informatio...@googlegroups.com.
> > To unsubscribe from this group, send email tojavascript-information-visu...@googlegroups.com.

Jim Klo

unread,
Feb 10, 2012, 12:14:43 AM2/10/12
to javascript-information...@googlegroups.com
Ok, you shouldn't need to call $.parseJSON() since you're inside the $.getJSON() callback, data should already be parsed into an object.

So if you do a console.log(data), you should see in the console the structure of data as an object.

- Jim
> You received this message because you are subscribed to the Google Groups "JavaScript InfoVis Toolkit" group.

> To post to this group, send email to javascript-information...@googlegroups.com.
> To unsubscribe from this group, send email to javascript-information-visua...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/javascript-information-visualization-toolkit?hl=en.
>
>

kishan

unread,
Feb 10, 2012, 12:21:05 AM2/10/12
to JavaScript InfoVis Toolkit
hi jim,

i get output in console as
Object {country="India", city="Mumbai"}

how do i send this json object into the init function to display on a
hypertree ??

-kishan
> andhttp://localhost/myservletviolatesthis too. If you use firebug and you
> tojavascript-informatio...@googlegroups.com.>> > To unsubscribe from this group, send email
>
> tojavascript-information-visu...@googlegroups.com.>> > For more options, visit this group athttp://
>
> groups.google.com/group/javascript-information-visualization-t....
>
> > --
> > You received this message because you are subscribed to the Google Groups
>
> "JavaScript InfoVis Toolkit" group.> To post to this group, send email to
>
> javascript-information...@googlegroups.com.> To unsubscribe from this group, send email to
>
> javascript-information-visua...@googlegroups.com.> For more options, visit this group at
>
> http://groups.google.com/group/javascript-information-visualization-t...
> .
>
>
>
>
>
>
>
>

Jim Klo

unread,
Feb 10, 2012, 2:51:08 AM2/10/12
to javascript-information...@googlegroups.com
I just remembered I have a public demo that I think still works:

http://demolearningregistry.sri.com/browse/

It's a complex example given the amount of data manipulation done, but it should illustrate what you're trying to do when loading json (jsonp in mine since it does cross origin stuff) via remote call.

- Jim
>> >> >>>>>     > tojavascript-informatio...@googlegroups.com.>> > To unsubscribe from this group, send email
>>
>> tojavascript-information-visu...@googlegroups.com.>> > For more options, visit this group athttp://
>>
>> groups.google.com/group/javascript-information-visualization-t....
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups
>>
>> "JavaScript InfoVis Toolkit" group.> To post to this group, send email to
>>
>> javascript-information...@googlegroups.com.> To unsubscribe from this group, send email to
>>
>> javascript-information-visua...@googlegroups.com.> For more options, visit this group at
>>
>> http://groups.google.com/group/javascript-information-visualization-t...
>> .
>>
>>
>>
>>
>>
>>
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups "JavaScript InfoVis Toolkit" group.
> To post to this group, send email to javascript-information...@googlegroups.com.
> To unsubscribe from this group, send email to javascript-information-visua...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/javascript-information-visualization-toolkit?hl=en.
>
>

Brian

unread,
Feb 11, 2012, 6:18:57 AM2/11/12
to JavaScript InfoVis Toolkit
Finally, I have managed to get a JIT visualisation to work with
external .json file.
I am using a Space Tree as my visualisation but im sure this will work
with HyperTree. Below is how I have gone about it.

/*--------- here is my json file --------------*/
{"id":"one", "name":"John", "data":{"name":"John", "jobTitle":"Admin",
"fax":"Albert", "mobile":"mobile", "phone":"sssss",
"email":"jo.b...@test.co.uk"}, "children": []}



/*-------- javascript code ----------*/

//The Org Chart code
function init(json){

//START Create a new Space Tree instance
var st = new $jit.ST({
... //all the space tree code here
}); //END Create a new Space Tree instance


//load json data
st.loadJSON(json);
//compute node positions and layout
st.compute();
//emulate a click on the root node.
st.onClick(st.root);

}//END of init() function




//ajax call
var jsonurl = "orgchart-example.json";
$.ajax({
url : jsonurl,
type : "GET",
success: function(data) {
alert("Success: data: " + data);
var json = $.parseJSON(data);
console.log(json);
//alert(obj);
init(json); //this line will call init function with JSON loaded
after this call

},
error:function (xhr, ajaxOptions, thrownError){
alert("Status error: " +xhr.status);
alert("Error message: " +thrownError);
}
});

Hope this helps.
Brian

On Feb 10, 7:51 am, Jim Klo <jim....@gmail.com> wrote:
> I just remembered I have a public demo that I think still works:
>
> http://demolearningregistry.sri.com/browse/
>
> It's a complex example given the amount of data manipulation done, but it
> should illustrate what you're trying to do when loading json (jsonp in mine
> since it does cross origin stuff) via remote call.
>
> - Jim
>
>
>
>
>
>
>
>
>
> >> andhttp://localhost/myservletviolatesthistoo. If you use firebug and you
> http://groups.google.com/group/javascript-information-visualization-t...
> .
>
>
>
>
>
>
>
>
Reply all
Reply to author
Forward
0 new messages