Jump to content

Dynamically Build JSON Data


mjsulliv
 Share

Recommended Posts

Hello All. I've been hammering on some code that I thought would work but can't seem to get it to do so. I want to dynamically build the JSON data I send when the OnClick of a check box is done. The function callsseem to work fine but I can't seem to successfully substitute the JSON data as a js var. Below is a snippet of code that I hope demonstrates what I'm trying to do. Any help would be appreciated.

function BuildJsonData(){   var jsonData = "{";  var recdNum_ckBx = document.getElementById('selectMenu_RecdNum_CkBx');  if(recdNum_ckBx.checked == true){	jsonData = '"RecdNum_ckBx" : "RecdNum_ckBx"';  }  var  recdTimeStmp_CkBx = document.getElementById('selectMenu_RecdTimeStmp_CkBx');  if(recdTimeStmp_CkBx.checked == true){	jsonData = jsonData + "," + ' "RecdTimeStmp_ckBx" : "RecdTimeStmp_ckBx"';  }  jsonData = jsonData + "}";  $.getJSON("selectMenu_BldSelPhrase.php", 	{	  jsonData;	}  );}

Link to comment
Share on other sites

You are correct to build your string using expressions likejsonData = jsonData +It looks like your second assignment to jsonData does not do that, jsonData = '"RecdNum_ckBx" : "RecdNum_ckBx"';causing the "{" character to be overwritten. I personally like to use the += operator.

Link to comment
Share on other sites

You are correct to build your string using expressions likejsonData = jsonData +It looks like your second assignment to jsonData does not do that, jsonData = '"RecdNum_ckBx" : "RecdNum_ckBx"';causing the "{" character to be overwritten. I personally like to use the += operator.
Thanks, I'll make the correction and give it a try.
Link to comment
Share on other sites

You are correct to build your string using expressions likejsonData = jsonData +It looks like your second assignment to jsonData does not do that, jsonData = '"RecdNum_ckBx" : "RecdNum_ckBx"';causing the "{" character to be overwritten. I personally like to use the += operator.
I've corrected the code:
function BuildJsonData(){   var jsonData = "{";  var recdNum_ckBx = document.getElementById('selectMenu_RecdNum_CkBx');  if(recdNum_ckBx.checked == true){	jsonData = jsonData + '"RecdNum_ckBx" : "RecdNum_ckBx"';	a = 1; // line for debug breakpoint  }  var  recdTimeStmp_CkBx = document.getElementById('selectMenu_RecdTimeStmp_CkBx');  if(recdTimeStmp_CkBx.checked == true){	jsonData = jsonData + "," + ' "RecdTimeStmp_ckBx" : "RecdTimeStmp_ckBx"';	a = 1; // line for debug breakpoint  }  jsonData = jsonData + "}";  return jsonData;

According to Netbeans debugger what I get being passed in the #_GET array is:[{"RecdNum_ckBx"_:_"RecdNum_ckBx",_"RecdTimeStmp_ckBx"_:_"RecdTimeStmp_ckBx"}]Where did the "_" characters come from? And because of them the JSON data is not being parsed correctly. I expect this code would have generated two $GET array values$GET[RecdNum_ckBx] having the value: "RecdNum_ckBx"$GET[RecdTimeStmp_ckBx] having the value: "RecdTimeStmp_ckBx"Any ideas?

Link to comment
Share on other sites

What does the $.getJSON function do? Are you sure that expects a JSON string and not an object? If jsonData is a string, then this syntax looks strange to me:

  $.getJSON("selectMenu_BldSelPhrase.php", 	{	  jsonData;	}  );

That says that you're calling the $.getJSON function, and you're passing it a string filename, and then an object that contains a single unnamed property, which is a string. I don't even think that will parse, you need to give the property a name. The semicolon after jsonData also doesn't belong there.If your library, whether that is jQuery or mootools or whatever, has support for JSON, then you can probably build an object and then convert the object to a JSON string instead of trying to build the JSON string yourself.

Link to comment
Share on other sites

what does jsonData look like before you return it from the function?
in Firebug: inside the function: "{"RecdNum_ckBx" : "RecdNum_ckBx", "RecdTimeStmp_ckBx" : "RecdTimeStmp_ckBx"}"back in the calling code: "{"RecdNum_ckBx" : "RecdNum_ckBx", "RecdTimeStmp_ckBx" : "RecdTimeStmp_ckBx"}"
Link to comment
Share on other sites

What does the $.getJSON function do? Are you sure that expects a JSON string and not an object? If jsonData is a string, then this syntax looks strange to me:
  $.getJSON("selectMenu_BldSelPhrase.php", 	{	  jsonData;	}  );

That says that you're calling the $.getJSON function, and you're passing it a string filename, and then an object that contains a single unnamed property, which is a string. I don't even think that will parse, you need to give the property a name. The semicolon after jsonData also doesn't belong there.If your library, whether that is jQuery or mootools or whatever, has support for JSON, then you can probably build an object and then convert the object to a JSON string instead of trying to build the JSON string yourself.

You are right, the semicolon is wrong and is not really in the code i'm running; probably vestigial from a early cut and paste.I'm using jQuery. What I think the function is expecting is a JSON object. How would i make the object instead of a string?
Link to comment
Share on other sites

var obj = {  RecdNum_ckBx: "value 1",   RecdTimeStmp_ckBx: "value 2"};

That's a generic object with the named properties and their values. jQuery should have a method for converting that object to a JSON string. I don't use jQuery so I'm not sure what the syntax is, but it may be something like this:var json = obj.toJSON();var json = $.stringify(obj);"stringify" is a common name for turning a Javascript object into a JSON string. Turning the string back into an object is parsing.From what I can tell, strangely enough, I don't think jQuery includes a way to serialize an object. You can parse a string, but you can't serialize an object. That's a little odd, the people here say to use an external library for that.http://stackoverflow.com/questions/191881/...-json-in-jquery

Link to comment
Share on other sites

var obj = {  RecdNum_ckBx: "value 1",   RecdTimeStmp_ckBx: "value 2"};

That's a generic object with the named properties and their values. jQuery should have a method for converting that object to a JSON string. I don't use jQuery so I'm not sure what the syntax is, but it may be something like this:var json = obj.toJSON();var json = $.stringify(obj);"stringify" is a common name for turning a Javascript object into a JSON string. Turning the string back into an object is parsing.From what I can tell, strangely enough, I don't think jQuery includes a way to serialize an object. You can parse a string, but you can't serialize an object. That's a little odd, the people here say to use an external library for that.http://stackoverflow.com/questions/191881/...-json-in-jquery

Thank you. Your insights are helpful. I found description of obj to string and back at: http://www.json.org/js.htmlI'll start playing w/ these.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...