0

JSONオブジェクトを受け入れる単純な変数があります...

すべてのコードは次のとおりです。

私が作業している場所を公開しないのに十分きれいであると判断したので、これは役立つはずです。

var server = "00.00.000.00:8800";
var webServices = "http://" + server + "/somedir/ws/";

var networksURL;
var sitesURL;
var resourcesURL;
var componentsURL;

networksURL = webServices + 'config/networks';
sitesURL =  webServices + 'config/sites';
resourcesURL =  webServices + 'config/resources';
componentsURL =  webServices + 'config/components';


var treeData;
var netsSites;
var sitesRes;
var resComp;

var myType;
var myUrl;
var jsonLen;
var myData1;
var JSONModified;
var dataToget;


function getInfo(myUrl, callback) {

    console.log("INSIDE GETINFO:: " + myUrl);

    var myData;
    $.ajax({
        type: 'GET',
        url: myUrl,
        async: true,
        crossDomain: true,
        //data: "{}",                       //NOT USED unless you have parameters
        //jsonpCallback: 'jsonpCallback',   //NOT USED unless the server is set up properly
        dataType: 'json',
        //Switch to jsonp ONLY if your JSON CALLBACK is set up properly on the server
        contentType: 'application/json',
        success: function(data) {
            jsonLen = data.length;
            myData1 = data;
            //If successful, show the data, if not, show error
            if (!data) {

                console.log("Error");
                //$("#alert_json").text("There was an error processing the your request.");
                return false;
            } else {

                myData = JSON.stringify(data);
                //**************************** STEP 1 ***************************
                //Now send to processing (STEP 1) add the prefixes for the JSTree
                JSONModified = addPrefix(myData);
                //console.log(JSONModified);
                //End sending back the data
                //**************************** END STEP 1 ***********************

                //console.log("Data found: " + myData);
                //$("#alert_json").text(JSONModified);
                if (JSONModified)
                {
                    callback(JSONModified);
                } else {
                    callback('[{"data":"No site data found..."}]');
                }
            }
        },
        error: function(data) {
            //console.log("Data not found or is not available at this time");
            //$("#alert_json").text("Data not found or is not available at this time");
            callback(null);
        }
    });
}

//PRELIMINARY - CALL THE FUNCTION passing in the JSON object you wish to retreive
function buildTree(typJSON) {

    myType = typJSON;


    //DETERMINE The type of JSON and then set it below.
    switch (typJSON) {

        case "nets":
            dataToget = networksURL;
            break;
        case "sties":
            dataToget = sitesURL;
            break;
        case "res":
            dataToget = resourcesURL;
            break;
        case "comp":
            dataToget = componentsURL;
            break;
        default:
            dataToget = 'Unknown Error';
            break;
    }



    getInfo(dataToget, function(data) {

        if (!data) {

            console.log("Return Value: NULL");
            return false;
        } else {

            //console.log("Return Value: SUCCESS!");
            //console.log(JSONModified);
            // FINAL STEP!
            switch (myType)
            {
                case "nets":
                    treeData = JSONModified;
                    return treeData; //SEND BACK THE JSON to the 
                    break;
                case "sites":
                    netsSites = JSONModified;
                    return netsSites; //SEND BACK THE JSON to the 
                    break;
                case "res":
                    sitesRes = JSONModified;
                    return sitesRes; //SEND BACK THE JSON to the 
                    break;
                case "comp":
                    resComp = JSONModified;
                    return resComp; //SEND BACK THE JSON to the 
                    break;
                default:
                    return '[{"data":"No tree data found..."}]';


            }
            //treeviewDetails.html to build the tree ONLY if called from tree
        }

    });
}


//**************************** STEP 1 ***************************
//Now process the JSON object and get the names of all the groups; albeit networks, sites, resources, components
function addPrefix(rawJSON) {

    var jsonArray = JSON.parse(rawJSON); // parse the JSON string to an actual array

    //Loop through all the groups of .... networks, sites, resources, components
    $.each(jsonArray, function(index, element) {

        element.data = element.name; //This can be INDEX or any other part...
        element.metadata = {
            id: element.id,
            note: element.notes
        };

        netsSites = _mySites(element.id);

        console.log("This is a site: " + netsSites);

        //If eval(getSites(element.id)) then make children
        if(netsSites !== "[]" || netsSites !== "")
        {
            element.children = eval(netsSites);
        }
    });
    console.log("Ret from GetInfo:  " + JSON.stringify(jsonArray));
    return JSON.stringify(jsonArray); // converts the array, with the new properties, back to a JSON string
}
//ERROR HANDLING
function errorHandler(e) {
    var msg = '';

    switch (e.code) {
        case FileError.QUOTA_EXCEEDED_ERR:
            msg = 'QUOTA_EXCEEDED_ERR';
            break;
        case FileError.NOT_FOUND_ERR:
            msg = 'NOT_FOUND_ERR';
            break;
        case FileError.SECURITY_ERR:
            msg = 'SECURITY_ERR';
            break;
        case FileError.INVALID_MODIFICATION_ERR:
            msg = 'INVALID_MODIFICATION_ERR';
            break;
        case FileError.INVALID_STATE_ERR:
            msg = 'INVALID_STATE_ERR';
            break;
        default:
            msg = 'Unknown Error';
            break;
    }

    console.Log('Error: ' + msg);
}


//In the basement, we're going to grab the sites, resources, and components
//With Callback...


    function _mySites(netID) {

        myNetwork = networksURL + "/" + netID + "/sites";

        console.log(myNetwork);

        return getInfo(myNetwork, function(data){});
    }

更新...上記のすべてのコードを参照してください...これが役立つはずです。

ありがとう。

4

2 に答える 2