By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,582 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

JSON Format - trouble using it for data interchange

nathj
Expert 100+
P: 938
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:
Expand|Select|Wrap|Line Numbers
  1. function generateJSON(pcIDList)
  2. {
  3.     // set the default return
  4.     lcReturn = "" ;
  5.  
  6.     lcJSONString = '{';    
  7.     if (pcObjectName != "" && pcIDList != "")
  8.     {
  9.         // take the data into a JSON structure                                          
  10.         laIDList = pcIDList.split(",");
  11.         for(lnPntr in laIDList)
  12.         {
  13.             lcElementID    = laIDList[lnPntr] ;                                 
  14.             lcElement    = document.getElementById(lcElementID) ; 
  15.             lcJSONString += lcElement.name + ':' + lcElement.value  ; 
  16.             if(lnPntr < (laIDList.length - 1))
  17.             {
  18.                 lcJSONString += ',' ;
  19.             }
  20.         }
  21.  
  22.         lcJSONString += '}'    ;
  23.         lcReturn = lcJSONString ;
  24.     }
  25.  
  26.     return lcReturn ;
  27. }
  28.  
The parameter pcIDList is a comma separated list of from control ID's. So if I pass in:
Expand|Select|Wrap|Line Numbers
  1. 'dataQuantity,sameGroup,groupNumber'
  2.  
from a form where I have asked for 5 sets of data all in group 6 the output is:
Expand|Select|Wrap|Line Numbers
  1.     {dataQuantity:1,sameGroup:false,groupSelect:2}
  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
Nov 22 '07 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Should that be groupNumber or groupSelect?

Why not just pass a string and let PHP parse the JSON?
Nov 27 '07 #2

nathj
Expert 100+
P: 938
Should that be groupNumber or groupSelect?
They should both be groupSelect
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
Nov 28 '07 #3

gits
Expert Mod 5K+
P: 5,390
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?

Expand|Select|Wrap|Line Numbers
  1. [{dataQuantity:1,sameGroup:false,groupSelect:2}]
kind regards
Nov 28 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.