我有几个json文件,它们都具有相同的结构,每个文件使用不同的年份:
#include <vector>
class Robot {
private:
public:
void move_right();
void move_left();
void switch_camera();
void raise_camera();
};
struct Action
{
Action(void (Robot::*what)(void))
: what(what)
{}
void perform(Robot& who) const
{
(who.*what)();
}
void (Robot::*what)(void);
};
bool should_abort();
void perform_actions(Robot& who, std::vector<Action> const& actions)
{
for (auto&& action : actions)
{
if (should_abort()) break;
action.perform(who);
}
}
int main()
{
std::vector<Action> actions {
&Robot::move_right,
&Robot::raise_camera,
&Robot::switch_camera,
&Robot::move_left
};
Robot r;
perform_actions(r, actions);
}
它们分别称为data_2014.json,data_2015.json和data_2016.json。
使用PHP,我想要的最终json是alldata.json,如下所示:
{"AEK":{"country":"Greece","shtv":"VEK"} ,
"BER":{"country":"Germany","shtv":"BKE"} ,
"CAR":{"country":"Italy","shtv":"CRA"}}
{"AEK":{"country":"Greece","shtv":"MOR"} ,
"DAR":{"country":"Turkey","shtv":"DDR"}}
{"AEK":{"country":"Greece","shtv":"MIL"} ,
"BER":{"country":"Germany","shtv":"BKE"} ,
"CAR":{"country":"Italy","shtv":"KUN"}}
我的意思是:对于重复的元素,获取可用的最新信息(即,对于AEK元素,获取属性“ shtv” =“ MIL”,这是data_2016.json中的元素。对于不再重复,只需获取可用信息即可。
答案 0 :(得分:4)
您需要使用json_decode
将JSON转换为数组,然后使用array_merge
合并所有json数组,然后使用json_encode
再次编码以将所有json合并为一个。
$json1 = '{"AEK":{"country":"Greece","shtv":"VEK"} ,
"BER":{"country":"Germany","shtv":"BKE"} ,
"CAR":{"country":"Italy","shtv":"CRA"}}';
$json2 = '{"AEK":{"country":"Greece","shtv":"MOR"} ,
"DAR":{"country":"Turkey","shtv":"DDR"}}';
$json3 = '{"AEK":{"country":"Greece","shtv":"MIL"} ,
"BER":{"country":"Germany","shtv":"BKE"} ,
"CAR":{"country":"Italy","shtv":"KUN"}}';
$arr1 = json_decode($json1,true);
$arr2 = json_decode($json2,true);
$arr3 = json_decode($json3,true);
$finalArr = array_merge($arr1,$arr2,$arr3);
$final_json = json_encode($finalArr);
echo $final_json;
答案 1 :(得分:0)
首先,您必须使用json_decode()解码json值。
$var1= '{"AEK":{"country":"Greece","shtv":"VEK"} ,
"BER":{"country":"Germany","shtv":"BKE"} ,
"CAR":{"country":"Italy","shtv":"CRA"}}';
$var2= '{"AEK":{"country":"Greece","shtv":"MOR"} ,
"DAR":{"country":"Turkey","shtv":"DDR"}}';
$var3= '{"AEK":{"country":"Greece","shtv":"MIL"} ,
"BER":{"country":"Germany","shtv":"BKE"} ,
"CAR":{"country":"Italy","shtv":"KUN"}}';
并使用json_decode($ var,TRUE)传递true,因为 TRUE ,返回的对象将转换为关联数组。
$array1= json_decode($var1,true);
$array2= json_decode($var2,true);
$array3 =json_decode($var3,true);
然后,您具有这三个数组值的array_merge(),您将获得所需的输出。
$ result = json_encode(array_merge($ array1,$ array2,$ array3));
$ result返回
{"AEK":{"country":"Greece","shtv":"MIL"},
"BER":{"country":"Germany","shtv":"BKE"},
"CAR":{"country":"Italy","shtv":"KUN"},
"DAR":{"country":"Turkey","shtv":"DDR"}
}
答案 2 :(得分:-1)
$json1_str = file_get_contents('data_2014.json');
$json2_str = file_get_contents('data_2015.json');
$json3_str = file_get_contents('data_2016.json');
$json1_array = json_decode($json1_str, true);
$json2_array = json_decode($json2_str, true);
$json3_array = json_decode($json3_str, true);
$array = array_merge($json1_array, $json2_array, $json3_array);
$final_output=json_encode($array);