I am new to PHP Programming, I need to parse the json string. This is the string what I have as JSON array
[{"datetime":"17/02/2014 13:18:30","type":"testtype","locationid":"1","GPSType":"GOOGLE","GPSLatitude":"1.1","GPSLongtitude":"1.2","userid":"admin","brand":"1234567","numbers":["num1","num2","num3]}]
In the above string I need to parse numbers array only.
2 Answers 2
I hope you are having JSON string in your PHP code. There is an error in your JSON string. Since you are maintaining your JSON string as an array, you may follow this approach. You can understand this easily.
<?php
$json = '[
{
"datetime": "17/02/2014 13:18:30",
"type": "testtype",
"locationid": "1",
"GPSType": "GOOGLE",
"GPSLatitude": "1.1",
"GPSLongtitude": "1.2",
"userid": "admin",
"brand": "1234567",
"numbers": ["num1", "num2", "num3"]
}
]';
$obj = json_decode($json);
$len = count($obj[0]->{'numbers'});
for($i = 0; $i < $len; $i++) {
echo $obj[0]->{'numbers'}[$i].'<br>';
}
?>
The Output would be:
num1
num2
num3
Comments
As others have mentioned in the comments, use json_decode.
http://www.php.net/manual/en/function.json-decode.php
[EDIT]
In order to get only the "numbers" array, simply try this:
$json = json_decode($input, true);
$numbers = $json[0]["numbers"];
The json_decode function will convert your JSON string to an associative array, from which you can just get the indices you need.
json_decode()- But make sure the JSON string is valid, else it'd returnNULL. The JSON string you have in the question is not valid (as you can verify with an online validator such as jsonlint.com)"in that comment that isn't in the question.