I have to parse this JSON in PHP so that for each address, I get the address, line and city information and store it in the dat开发者_运维百科abase. The way that I plan to implement is like this:
For each key in the json string, check if it begins with address,
If yes, split the string based on '_' and get the index count.
Get line1, line2, city for this index count.
Is there a better way to do this?
(Note that the index count can be random)
{
"route": "A",
"address_0": "A0",
"line1_0": "L1_0",
"line2_0": "L2_0",
"city_0": "city_0",
"address_1": "A1",
"line1_1": "L1_1",
"line2_1": "L2_1",
"city_1": "city_1",
"address_2": "A2",
"line1_2": "L1_2",
"line2_2": "L2_2",
"city_2": "city_2",
}
Just iterate over the data and assign to an array:
$route = array();
foreach(json_decode($json) as $p => $v)
{
list($k, $i) = explode('_', $p, 2) + array(NULL, NULL);
if (NULL === $i) {
$rc =& $route["$k $v"];
continue;
}
$rc[$i][$k] = $v;
}
unset($rc);
gives:
array(1) {
["route A"]=>
array(3) {
[0]=>
array(4) {
["address"]=>
string(2) "A0"
["line1"]=>
string(4) "L1_0"
["line2"]=>
string(4) "L2_0"
["city"]=>
string(6) "city_0"
}
[1]=>
array(4) {
["address"]=>
string(2) "A1"
["line1"]=>
string(4) "L1_1"
["line2"]=>
string(4) "L2_1"
["city"]=>
string(6) "city_1"
}
[2]=>
array(4) {
["address"]=>
string(2) "A2"
["line1"]=>
string(4) "L1_2"
["line2"]=>
string(4) "L2_2"
["city"]=>
string(6) "city_2"
}
}
}
Demo
Yes. Use this format instead
{
"route": "A",
data : [{
"address": "A0",
"line1": "L1_0",
"line2": "L2_0",
"city": "city_0"
},{
"address": "A1",
"line1": "L1_1",
"line2": "L2_1",
"city": "city_1"
},{
"address": "A2",
"line1": "L1_2",
"line2": "L2_2",
"city": "city_2"
}]
}
and get it like this
$json = ' {
"route": "A",
data : [{
"address": "A0",
"line1": "L1_0",
"line2": "L2_0",
"city": "city_0"
},{
"address": "A1",
"line1": "L1_1",
"line2": "L2_1",
"city": "city_1"
},{
"address": "A2",
"line1": "L1_2",
"line2": "L2_2",
"city": "city_2"
}]
}';
$object = json_decode($json);
echo $object->data[0]->address; // A0
echo $object->data[1]->address; // A1
//and so on...
This is a tricky one because even the JSON isn't formed very well (address_0 vs address_1) instead of something like
{
"route" :
{ "address" : "something", "line1": "something else" },
{ ... }
}
I would do a json_decode
to turn what you have into an array, then parse the keys appropriately and pass the values into a DB object (or loop and save).
You can always do this:
$json = {
"route": "A",
"address_0": "A0",
"line1_0": "L1_0",
"line2_0": "L2_0",
"city_0": "city_0",
"address_1": "A1",
"line1_1": "L1_1",
"line2_1": "L2_1",
"city_1": "city_1",
"address_2": "A2",
"line1_2": "L1_2",
"line2_2": "L2_2",
"city_2": "city_2",
}
$object = json_decode($json);
$route = array();
$currentRoute = "";
$addressCounter = 0;
foreach($object as $key => $value)
{
if($key == "route"){
$currentRoute = $value;
} else {
$explode = explode("_",$key);
$route[$currentRoute][$addressCounter][$explode[0]] = $value;
if($explode[0] == "city"){
$addressCounter++;
}
}
}
print_r($route)
// Should return something like
// Array (
// ['A'] => Array (
// [0] => Array (
// ['address'] => 'A0',
// ['line1'] => 'L1_0'
// ...
It's nasty, but it works. Again...if you have control over the original form, I would just rethink that approach to better be handled here.
Good luck!
精彩评论