0

I have an piece of hardware that returns temperature 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}]}

3 Answers 3

2

Simply get it in an array:

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

Simple/Perfect/Best ;-)

Sign up to request clarification or add additional context in comments.

Comments

0

It think it is in JSON format.

2 Comments

Strictly speaking, JSON would require the names in the name/value pairs to be quoted, ie "label" rather than label. See the json_decode() docs, Example #3. My PHP 5.3.3 cannot parse your string.
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?
0

You can parse it using PEAR package - Services_JSON

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.