Load Json file

268 views
Skip to first unread message

Long Zachary

unread,
Mar 26, 2013, 12:42:36 PM3/26/13
to javascript-information...@googlegroups.com
Hi all,

I have really go crazy on one problem, how could I load the json data from another source.

I mean, I have a function to generate a json data in back, and get the data in a JSP webpage, as I do alert() function, I could see the data is perfected structured in the screen. But as I use a variable to storage it and use the infoVis to plot it, it only plot one point in the centre also with messy code (BTW, I am doing a small project in Chinese )

please, tell me how could I input the json data that generated outside the js function.

Thanks a lot in advance.

Best Regards,
Long 

Scott

unread,
Mar 26, 2013, 6:30:47 PM3/26/13
to javascript-information...@googlegroups.com
Hi Long,

You could just write a JSON file onto your web server and then use an Ajax call in your js to get the JSON into a js variable. For example (assuming use of JQuery):
        var json = null;
        var fd = new $jit.ForceDirected({...
       
        $.ajax({
            type: 'GET',
            url: '/script-to-get-JSON-string.php,
            dataType: 'json',
            async: false,
        success: function(data)
        {
            json = data;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('error obtaining data from server');
            return;
        }
        });

fd.loadJSON(json);

        fd.computeIncremental({
            iter: 5,
            property: 'end',
               onComplete: function(){
                 fd.animate({
                    modes: ['linear'],
                    transition: $jit.Trans.Elastic.easeOut,
                    duration: 5000
                  });
            }
            });

You could also use jQuery's getJSON call if easier (see http://api.jquery.com/jQuery.getJSON/)

Scott.

Long Zachary

unread,
Mar 28, 2013, 4:57:56 AM3/28/13
to javascript-information...@googlegroups.com
Dear Scott,

First thank you very very much. and now I have take the json data from server into the page, and I do the alert(json) in the JSP page, it could displayed very well, but as us this variable, it only plot a point in the centre, named "undefined".

As I replace the json variable into the particular string, that displayed by the alert(), it works very well.

Do you know how to fix this, thanks a lot in advance.

Best Regards,
Long

在 2013年3月27日星期三UTC+8上午6时30分47秒,Scott写道:

Scott

unread,
Apr 2, 2013, 6:27:57 PM4/2/13
to javascript-information...@googlegroups.com
You probably need to post the relevant code as it's difficult to say what's wrong without seeing the code. If you're getting 'undefined' that would indicate a problem with the variable scope, the JSON object is not being assigned correctly, you are not referencing the right variable or something else altogether.

Long Zachary

unread,
Apr 2, 2013, 11:07:41 PM4/2/13
to javascript-information...@googlegroups.com
Dear Scott, 

Thank you very much, I have solve the problem, the reason why it comes as a undefined is that, I have send a String variable from back to front, but in fact the function needs a object variable.

Cheers,
Long

在 2013年4月3日星期三UTC+8上午6时27分57秒,Scott写道:

Ricky

unread,
Apr 4, 2013, 9:29:26 PM4/4/13
to javascript-information...@googlegroups.com
long I spend hours trying to figure it out, that eval() function! good luck
Reply all
Reply to author
Forward
0 new messages