skip to Main Content

I’ve tried the bstreeview and I’m able to get the example in github to show the demo three. And when I change the json variable it updates correspondingly. E.g. this code shows up fine:

            let myJsonVar = [{ text: "Inbox", icon: "fa fa-inbox fa-fw" }]
            $('#myTree').bstreeview({
                data: myJsonVar,
                expandIcon: 'fa fa-angle-down fa-fw',
                collapseIcon: 'fa fa-angle-right fa-fw',
                indent: 1.25,
                parentsMarginLeft: '1.25rem',
                openNodeLinkOnNewTab: true
            });

However, in my program, the three comes in string form, so I thought I could just invoke ‘parseJSON() to convert it. But for some reason it does not work:

            let sTxt = '[{ text: "Inbox", icon: "fa fa-inbox fa-fw" }]';
            let myJsonVar = parseJSON(sTxt);
            $('#myTree').bstreeview({
                data: myJsonVar,
                expandIcon: 'fa fa-angle-down fa-fw',
                collapseIcon: 'fa fa-angle-right fa-fw',
                indent: 1.25,
                parentsMarginLeft: '1.25rem',
                openNodeLinkOnNewTab: true
            });

It should be such a dead easy thing, and I feel really bad posting for it, but I’ve really spent much time looking for the hidden trick (stringify, with following parseJSON or even two times parseJSON as I’ve seen some suggesting in other posts, the jQuery version, $.parseJSON, etc.) Has anybody else encountered this problem or is it only me having two left hands?

2

Answers


  1. Chosen as BEST ANSWER

    Posting an update on this, because I'm a bit further now thanks to your help, but there is still someting 'strange' going on that I'm not able to figure out of:

    Case A:

    If I create the string like this the bstreeview is now displaying fine:

                let sTxt2 = '[{"text": "Inbox", "icon": "fa fa-inbox fa-fw"}]';
                let sTxt = JSON.parse(sTxt2);
                $('#myTree').bstreeview({
                    data: sTxt,
                    expandIcon: 'fa fa-angle-down fa-fw',
                    collapseIcon: 'fa fa-angle-right fa-fw',
                    indent: 1.25,
                    parentsMarginLeft: '1.25rem',
                    openNodeLinkOnNewTab: true
                });
    

    Case B:

    however, in my program, the string is coming in through ajax and I'm picking it up through the success response like this:

                ....
                success: function (response) {
                    sTxt1 = response.msg.toString().trim();
                    alert(sTxt1);
                    let sTxt2 = JSON.parse(sTxt1);
                    alert(sTxt2);
                    ....
    

    In 'Case B', even though I send the exact same string in through Ajax the 'JSON.parse(sTxt1)' function will crash and the execution never reaches the second alert function 'alert(sTxt2)'. However, if I pick the Ajax success response text up from the first alert popup, by copying the text from the popup and pasting it into a variable, and run it like in 'Case A' it will work... and no matter if I remove the alert functions the execution never reaches the code further donw either, so it seems like it's the JSON.parse function that is crashing, but I'm really not able to see why.


  2. Found out of this myself, and indeed was dead easy as I thought, and even embarrasing. Anyhow, in case anybody else out there are js novices, and as head less as I can be, the solution was the single quotes in the start and end of the string. The way I found out of it was using try /catch (voila dead easy … and embarrasing). Sorry for having posted for this:

    try
    {
      ... 
      let myJsonVar = parseJSON(sTxt);
      ...
    }
    catch (e)
    {
      alert(e);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search