JSON Format - trouble using it for data interchange

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    JSON Format - trouble using it for data interchange

    Hi everyone,

    I am currently working on a project where data is being passed from the cilent side to the server side quite a lot. I thought that AJAX and JSON would be perfect for this. I have written a small function to generate what I thought was JSON data:
    [code=javascript]
    function generateJSON(pc IDList)
    {
    // set the default return
    lcReturn = "" ;

    lcJSONString = '{';
    if (pcObjectName != "" && pcIDList != "")
    {
    // take the data into a JSON structure
    laIDList = pcIDList.split( ",");
    for(lnPntr in laIDList)
    {
    lcElementID = laIDList[lnPntr] ;
    lcElement = document.getEle mentById(lcElem entID) ;
    lcJSONString += lcElement.name + ':' + lcElement.value ;
    if(lnPntr < (laIDList.lengt h - 1))
    {
    lcJSONString += ',' ;
    }
    }

    lcJSONString += '}' ;
    lcReturn = lcJSONString ;
    }

    return lcReturn ;
    }
    [/code]
    The parameter pcIDList is a comma separated list of from control ID's. So if I pass in:
    Code:
    'dataQuantity,sameGroup,groupNumber'
    from a form where I have asked for 5 sets of data all in group 6 the output is:
    Code:
    	{dataQuantity:1,sameGroup:false,groupSelect:2}
    I'm not sure this is correct, but all my reading around on the subject has left me rather confused, so I'm seeking help. The final part of my current problem is how then to pass this data set from my JavaScript to my PHP so that it can be parsed correctly and used to control the process flow and eventually update the database.

    Many thanks
    nathj
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Should that be groupNumber or groupSelect?

    Why not just pass a string and let PHP parse the JSON?

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by acoder
      Should that be groupNumber or groupSelect?
      They should both be groupSelect
      Originally posted by acoder
      Why not just pass a string and let PHP parse the JSON?
      Hi acoder,

      This is waht I am trying to do, generate a string of data that is JSON formatted so that PHP can parse it for me. the plane was then to send the string as part of the query string to the php file.

      However, the output I generate doesn't parse with json_decode(). I have tried it with true and false as the second parameter but alas the results is null.

      Do you ave any suggestions on how to generate the JSON formatted string so that I can pass it to the PHP file for processing?

      Many thanks
      nathj

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        i'm not quite sure but try to send it the following way, i think i remeber that encode always delivers it as an array so i assume decode expects an array too?

        Code:
        [{dataQuantity:1,sameGroup:false,groupSelect:2}]
        kind regards

        Comment

        Working...