I am using ColdFusion to Ajax data from a cfc and returning the data serialized. The console shows the data in this format.
query('id':[1],'title':['value'],'descr':['value2'])
How do I parse this?
I am using ColdFusion to Ajax data from a cfc and returning the data serialized. The console shows the data in this format.
query('id':[1],'title':['value'],'descr':['value2'])
How do I parse this?
This is a custom format which isn't easy to parse but with a few changes, you can convert it to JSON:
query(
with {
)
with }
"
with \"
'
with "
If you want to keep query
, then replace it with {"query":{
and replace )
with }}
Note: This is a very simple and therefore brittle solution. A better solution would be to get your server side framework to produce proper JSON and send that to the browser.
EDIT If you have a JSON string, you can use jQuery.parseJSON() to parse it.
The format your data is serialized makes parsing it to JSON really easy. See the jsfiddle for a live example. I started with getting the raw data as a string. Next I replaced the single quotes and added curly brackets around the string to fit the JSON string format.
Step 1
query('id':[1],'title':['value'],'descr':['value2'])
to
'id':[1],'title':['value'],'descr':['value2']
Step 2
'id':[1],'title':['value'],'descr':['value2']
to
{"id":[1],"title":["value"],"descr":["value2"]}
Now you can use JSON.parse to create a javascript object from the serialized string. But note that all values in your format are array values. This is a little example to show what I mean:
{
"int": 1,
"boolean": true,
"string": "foo",
"array": ["value1", true, 1234]
}
In your example every value is an array value, but JSON.parse parsed them as single values if the array contains only one element.