passing a variable into a json collection - javascript

I'm trying to call the following js method. I wish to pass in the variable siteid. I can alert this value. But it doesn't seem to work in the following context. ie if just add the id 1234 it works.
alert(siteid);
embedSWF ('flashcontent', '{"siteID":siteid,"siteType":"portal","mainSWF":"http:\/\/tv.xxx.net\/flash\/xxx1.swf","movieWidth":"426","movieHeight":"276","expressInstall":"http:\/\/tv.xxx.net\/expressInstall.swf"}');

You need to concatenate the value of siteid into your string: '...' + siteid + '...'
Depending on what you're building, you may want the string to have quotes around the value.

The most robust way to do this is to first make the object as a normal object and then serialize it:
var parms = {siteID: siteid, siteType: "portal", ...};
embedSWF('flashcontent', JSON.stringify(parms));
If you need to support outdated browsers that don't have the built-in JSON object, there are several implementations available online.

embedSWF ('flashcontent', '{"siteID":'+siteid+',"siteType":"portal","mainSWF":"http:\/\/tv.xxx.net\/flash\/xxx1.swf","movieWidth":"426","movieHeight":"276","expressInstall":"http:\/\/tv.xxx.net\/expressInstall.swf"}');

You're passing a string:
'{"siteID":siteid,"siteType":"portal","mainSWF":"http:\/\/tv.xxx.net\/flash\/xxx1.swf","movieWidth":"426","movieHeight":"276","expressInstall":"http:\/\/tv.xxx.net\/expressInstall.swf"}'
You probably mean to pass:
'{"siteID":' + siteid + ',"siteType":"portal","mainSWF":"http:\/\/tv.xxx.net\/flash\/xxx1.swf","movieWidth":"426","movieHeight":"276","expressInstall":"http:\/\/tv.xxx.net\/expressInstall.swf"}');
It seems a little weird to pass a JSON string, rather than the associative array itself. Either way, that's your problem, above.

Related

getComputedStyle Font Family returns multiple families [duplicate]

Hi I have an object rowObject passed into a javascript function. When I inspect it by putting it into an alert I see something like:
435,345,345345,56456
What I want to achieve is to get the first integer ie. 435 from the list.
I know how to do this in server side code but client side code.
Can someone please help me with this?
Assuming that your rowObject is a string, you can use .split() to split the comma delimited list. At this point you can access the array of items by index and get the first element.
http://www.w3schools.com/jsref/jsref_split.asp
An Example
var rowObject = "435,345,345345,56456";
var splitRowObject = rowObject.split(',');
if(splitRowObject.length > 0)
alert(splitRowObject[0]);
alert() is calling objects toString() method, so you don't know the structure of the object. It is a good idea to use console.log instead for logging objects as in modern browsers it will allow you to explore structure of the object in the console window.
One of the solutions you can do without knowing the structure is:
var firstInteger = +rowObject.toString().split(',')[0] // 435
That works if rowObject is string, array or everything else :).
EDIT: Putting + before the string will try to convert it to a number.
Have you tried rowObject[0]?
The fact that the alert shows 435,345,345345,56456 doesn't mean that the object is string, it could be Object and Array as well as their toString method implemented to display it in such way. For example the native array is also looks like that when alerting or converting to string, so you need to call toString method at first then split it by comma:
var firstInt = rowObject.toString().split(',')[0];

What is the right way to store JavaScript source code in a json object?

I want to edit JavaScript in a textarea and store it back into a JavaScript object. For example I have this object:
var item1 = {
'id' : 1,
'title':'title',
'sourcecode' : "alert('hallo')"
};
If I would change the content to alert("hallo") or a even more complex example does this break my object?
I would think there is some escape function like this https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/escape. But it is marked as deprecated.
So if this is deprecated what would be the right way for storing complex JavaScript code into a JavaScript object?
Should I use stringify ?
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
The JSON.stringify() method converts a JavaScript value to a JSON
string, optionally replacing values if a replacer function is
specified, or optionally including only the specified properties if a
replacer array is specified.
This does not read like there is an automated escape build in.
If you need to send the data to a server, I'd say you should encodeURI your sourceCode, and then JSON.stringify the entire object. When retreiving data from the server, you should decodeURI the sourceCode

javascript array into object with same key names

I have an unusual problem to solve here. I have an array of Guids
[
"c01f8237-72c8-4fa6-9c53-1915750385aa",
"2c8a471b-c408-436c-81b1-3f3867d8ffb4",
"27a44d46-12bd-4784-ceed-57ada31b0e33"
]
This array has to be transformed into:
{
id: "c01f8237-72c8-4fa6-9c53-1915750385aa",
id: "2c8a471b-c408-436c-81b1-3f3867d8ffb4",
id: "27a44d46-12bd-4784-ceed-57ada31b0e33"
}
I know that shouldn't be done, but unfortunately cannot control the back end part. Any idea?
Thanks
The whole point of a dictionary key is that it uniquely maps to some value. Your desired output attempts to duplicate a key and therefore is neither possible nor does it make sense.
If you're passing this to a backend (as you suggest), then of course you can manually build a string to pass over the wire that duplicates keys in the payload, but you won't be able to actually do it in JavaScript first. You'll have to manually build the string.
Also note that you can call this format whatever you want, but you can't call it JSON and you can't use JSON libraries to build it (because it's not JSON). If your API expects a custom format, then you need to write code to build that custom format.
If all you want is a string just do something like
var str = '{' + idArray.map(function(id) {
return "id: "+id
}).join(',\n')+'}';
I have no idea what mime type you would put on that though since its not valid JSON.

Get first item from comma delimited object

Hi I have an object rowObject passed into a javascript function. When I inspect it by putting it into an alert I see something like:
435,345,345345,56456
What I want to achieve is to get the first integer ie. 435 from the list.
I know how to do this in server side code but client side code.
Can someone please help me with this?
Assuming that your rowObject is a string, you can use .split() to split the comma delimited list. At this point you can access the array of items by index and get the first element.
http://www.w3schools.com/jsref/jsref_split.asp
An Example
var rowObject = "435,345,345345,56456";
var splitRowObject = rowObject.split(',');
if(splitRowObject.length > 0)
alert(splitRowObject[0]);
alert() is calling objects toString() method, so you don't know the structure of the object. It is a good idea to use console.log instead for logging objects as in modern browsers it will allow you to explore structure of the object in the console window.
One of the solutions you can do without knowing the structure is:
var firstInteger = +rowObject.toString().split(',')[0] // 435
That works if rowObject is string, array or everything else :).
EDIT: Putting + before the string will try to convert it to a number.
Have you tried rowObject[0]?
The fact that the alert shows 435,345,345345,56456 doesn't mean that the object is string, it could be Object and Array as well as their toString method implemented to display it in such way. For example the native array is also looks like that when alerting or converting to string, so you need to call toString method at first then split it by comma:
var firstInt = rowObject.toString().split(',')[0];

missing ] after element list parsing JSON

Executing that:
$.post(
"/url/to/method",
{ },
function(data){
var obj2 = eval("("+$(data).children()+")");
// OR var obj = $.evalJSON($($(data).children())); // Jquery-json
$body = $("#AAA");
$body.html(obj.fied);
},
"xml"
);
while turn into a "missing ] after element list" (at row 5 or 6) error in firebug. The JSON output from method has been validated with jsonlint.com/
Probably is obvious but please I'm newbie around AJAX/JSON. Thanks
What is probably happening is that your $.post call is returning a JSON object already. jQuery will try to detect JSON automatically and parse it for you. When you call eval on a JSON object like this, you see this error. Sneaky! Just use the data object as is.
Use the JSON.parse method, or be sure to include a space next to your parens before passing it to eval...
eval(" (" + data + ") ");
Are you writing this in the .NET 2.0? If so try constructing your data into a query string rather than passing a JSON object.
Example:
var1=alma&var2=user
Where "var1" and "var2" are the names of the parameters that your webmethod expects. Pass this using the post.. Data: {querystring}
I ran into issues when passing JSON to webservice in 2.0.
Good Luck
Turns out that jQuery will try to figure out if the result is JSON and parse it automatically, but in this case data is already a JavaScript object and not a JSON string waiting to be parsed. So you can use this object as it is instead of using eval() and JSON.parse.

Categories

Resources