How to remove the unwanted nested keys from JSON

2019-09-14 17:49发布

问题:

This is my json:

{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}

I want to remove the second level keys (e.g "26:" and "28":) using PHP.

In other words, I want to replace the double-quoted number keys with zero-indexed numeric keys.

How can I make it look like this:

{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}

回答1:

Here is the demo.

Code:

// declare $json
$array=json_decode($json,true);  // decode as array
// overwrite subarray with zero-indexed keys while preserving subarray values
$array['all_counts_reports']=array_values($array['all_counts_reports']);
var_export(json_encode($array));  // return to json

Input:

$json='{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}';

Output:

'{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}'

In your javascript, use JSON.parse() to strip the wrapping single quotes:

var str='{"all_counts_reports":[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]}';
var json=JSON.parse(str);
console.log(json);


And because we are running with assumptions, if you want to remove the all_counts_reports key as well, you can use this one-liner:

Code:

$new_json=json_encode(array_values(current(json_decode($json,true))));

Output:

'[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]'


回答2:

$array = json_decode($your_json_string,true);
print_r($array);
$result = array();
foreach($array['all_counts_reports'] as $rep){
    $result['all_counts_reports'][] = array(
        "name"=>$rep['name'],
        "date"=>$rep['date'],
        "trips_per_day"=>$rep['trips_per_day'],
        "cash_trips"=>$rep['cash_trips'],
        "credit_trips"=>$rep['credit_trips'],
        "compliment_trips"=>$rep['compliment_trips'], 
    );
}

$result_json = json_encode($result);
echo $result_json;

There may be better solution, but this one is right now in my mind

Its unclear that what you looking for, if you just want to remove and dont want to maintain as original json then you can do like this example. But if you dont want your all_counts_reports treated as array [] then this example will not help.

And simply pass to android then above will work.