views:

51

answers:

3

Hello,

I have an piece of hardware that returns tempature data etc. I am not sure what format this is though. I would like to parse it with php any suggestions on the best or easiest way to parse the data. I will be storing the data in a mysql database and this data will be inserted one every 30 seconds.

Below is the data:

{name:"Enviro",date:"07/22/10 17:32:34",uptime:"0d 19:06:28",scale:0,macaddr:"00:XX:XX:XX:XX:XX",devtype:"24",refresh:"30",sensor:[{label:"Rack Temp",tempf:"70.88",tempc:"21.60",highf:"72.50",highc:"22.50",lowf:"69.98",lowc:"21.10",alarm:1,type:38,enabled:1,humid:"37.27",highh:"45.72",lowh:"34.01",heati:"76.79",heatic:"24.88",highhi:"77.07",highhic:"25.03",lowhi:"76.57",lowhic:"24.76",hienabled:0},{label:"Ext. Sensor 1",tempf:"32.00",tempc:"0.00",highf:"32.00",highc:"0.00",lowf:"32.00",lowc:"0.00",alarm:0,type:0,enabled:0},{label:"HVAC",tempf:"46.27",tempc:"7.93",highf:"67.87",highc:"19.93",lowf:"43.70",lowc:"6.50",alarm:1,type:16,enabled:1},{label:"Ext. Sensor 3",tempf:"32.00",tempc:"0.00",highf:"32.00",highc:"0.00",lowf:"32.00",lowc:"0.00",alarm:0,type:0,enabled:0},{label:"Ext. Sensor 4",tempf:"32.00",tempc:"0.00",highf:"32.00",highc:"0.00",lowf:"32.00",lowc:"0.00",alarm:0,type:0,enabled:0},{label:"Ext. Sensor 5",tempf:"32.00",tempc:"0.00",highf:"32.00",highc:"0.00",lowf:"32.00",lowc:"0.00",alarm:0,type:0,enabled:0},{label:"Ext. Sensor 6",tempf:"32.00",tempc:"0.00",highf:"32.00",highc:"0.00",lowf:"32.00",lowc:"0.00",alarm:0,type:0,enabled:0}],switch_sen:[{label:"Air Flow",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0},{label:" ",enabled:1,alarm:1,status:0}]}

A: 

It think it is in JSON format.

Bakhtiyor
ok I was not sure, I will look into that.
digitalbart
it doesn't `json_decode()` for me.
Pekka
Strictly speaking, JSON would require the names in the name/value pairs to be quoted, ie `"label"` rather than `label`. See the [json_decode()](http://us3.php.net/json-decode) docs, Example #3. My PHP 5.3.3 cannot parse your string.
Adam Backstrom
It's not JSON since the keys to the objects are not quoted. It's very similar though (in fact, it looks like pure JS objects)... Perhaps it's time to build a JS object parser?
ircmaxell
+3  A: 

Simply get it in an array:

$str = '...';
$arr = json_decode(preg_replace('/([{,])([^:{,]+):/', '${1}"${2}":', $str));
var_dump($arr);

Simple/Perfect/Best ;-)

Ehsan
A: 

You can parse it using PEAR package - Services_JSON

dev-null-dweller