ColdFusion 7 and JSON Data: How to

  • 20
  • Jan 2006
  • Dev @ Contech Lab
  • View: 5710 | 0 Comments

  • coldfusion | json

  • How to use JSon encoded data with ColdFusion version 7? I have found a very little cfc to resolve this problem.

    Now, in the ColdFusion 8 there is a dedicated function to do this, but if u need to use this encoded data in Coldfusion 7 also, u can find useful this short inforrmation.


    The following code demostrate how to use:

    view plain print about
    <!--- code start ---------------- --->
    Encoding

    <cfscript>
    thestruct = StructNew();
    thestruct["realstring"] = "this is a real string";
    thestruct["integer"] = 1234;
    thestruct["FLOAT"] = 2.334;
    thestruct["substruct"] = structnew();
    thestruct.substruct["what"] = "this";
    thestruct.substruct["what1"] = "this";
    thestruct.substruct["what2"] = "this";
    thestruct.substruct["myarray"] = arraynew(1);
    thestruct.substruct.myarray[1] = "first";
    thestruct.substruct.myarray[2] = "second";
    thestruct.substruct.myarray[3] = "third";
    thestruct.substruct.myarray[4] = 'the fourth has a " quote in it, as well 
    as a comma';
    thestruct.substruct.myarray[5] = arraynew(1);
    thestruct.substruct.myarray[5][1] = "
    sub-1";
    thestruct.substruct.myarray[5][2] = "
    sub-2";
    thestruct.substruct.myarray[5][3] = "
    sub-3";
    </cfscript>

    view plain print about
    <cfinvoke component="JSON" method="encode" data="#thestruct#" 
    returnvariable="result" />


    <cfoutput>#result#</cfoutput>

    <Output:>
    <!--
    {"realstring":"this is a real string","FLOAT":2.334,"substruct":
    {"what2":"this","myarray":["first","second","third","the fourth has a \" 
    quote in it, as well as a comma",["sub-1","sub-2","sub-3"]],
    "what":"this","what1":"this"},"integer":1234}
    -->

    <Decoding>

    <cfset input_string = '{key1:"string value",key2:["array","of",
    "elements"],key3:123}' /
    >


    <cfinvoke component="JSON" method="decode" data="#input_string#" 
    returnvariable="result" />


    <cfdump var="#result#">

    <!--- code end ------------------------------- --->

    About CfJson

    More information