Saturday, November 18, 2017

JSON to Dictionary Conversion


Here is the tip to convert JSON Object to Dictionary.


private static Dictionary<string, object> GetDictFromJObject(JObject inJObject)
{
        if (inJObject == null) return new Dictionary<string, object>();
        return ((JObject)inJObject).ToObject<Dictionary<string, object>>();
}

JavaScript Function to get a Valid Number from String


Here is the function to get a numeric value from a string value.


    function GetValidNumberValue(inValue)
   {
        if (inValue == null) return 0.0;
        if (inValue == undefined) return 0.0;
        if (isNaN(inValue) == true) return 0.0;
        let togoNumber = parseFloat(inValue);
        if (isNaN(togoNumber) == true) return 0.0;
        return togoNumber;
    }

JavaScript functions to handle cookies


Here are the functions to handle cookies using JavaScript.

    function SetCookie(cookieName, cookieValue, exdays) {
        let d = new Date();
       d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        let expires = "expires=" + d.toUTCString();
        document.cookie = cookieName+ "=" + cookieValue+ "; " + expires;
    }

    function GetAllCookie() {
        return document.cookie;
    }

    function DelCookie(cname) {
        let name = cname + "=";
        document.cookie = cname + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
        return "";
    }

    function GetCookie(cname) {
        let name = cname + "=";
        let ca = document.cookie.split(';');
        for (let i = 0; i < ca.length; i++) {
           let c = ca[i];
           while (c.charAt(0) == ' ') c = c.substring(1);
           if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }


JavaScript Function to get the website URL Value


This JavaScript function will return the URL name along with the protocol ('http' or 'https') value.

function GetHost() {

        if (typeof location.protocol == 'undefined') {

            alert('location.protocol is undefined');

        }

        if (typeof location.host == 'undefined') {

            alert('location.protocol is undefined');

        }

        let host = location.protocol + '//' + location.host + '/';

        return host;

    }

Get JSON string from XML

Here is the function to convert XML string to JSON string.

string GetJSONStringFromXML(string inXMLString){
XDocument xDoc = XDocument.Parse(inXMLString); //orXDocument.Load(xmlfilepath)
string jsonString = JsonConvert.SerializeXNode(xDoc);
return jsonString;
}

All Blogs so far ...