代码之家  ›  专栏  ›  技术社区  ›  Javi

合并JSON文件,覆盖

  •  -2
  • Javi  · 技术社区  · 6 年前

    我有几个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"}}
    

    它们被称为data_2014.json、data_2015.json和data_2016.json。

    使用php,我希望得到的最后一个json是alldata.json,如下所示:

      {"AEK":{"country":"Greece","shtv":"MIL"} ,
       "BER":{"country":"Germany","shtv":"BKE"} ,
       "CAR":{"country":"Italy","shtv":"KUN"},
       "DAR":{"country":"Turkey","shtv":"DDR"}}
    

    我的意思是:对于重复的元素,获取可用的最新信息(即,对于AEK元素,获取属性“shtv”=“mil”,wich是data_2016.json中的一个。对于不重复的元素,只需获取可用的信息。

    3 回复  |  直到 6 年前
        1
  •  4
  •   Hardik Solanki    6 年前

    您需要使用 json_decode 然后使用 array_merge 然后用 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;
    
        2
  •  0
  •   Madhuri Patel    6 年前

    首先,您必须使用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,因为 真的 ,返回的对象将转换为关联数组。

    $array1= json_decode($var1,true);
    $array2= json_decode($var2,true);
    $array3 =json_decode($var3,true);
    

    然后您将得到这三个数组值的array_merge(),您将得到所需的输出。

    $result=json_encode(数组_merge($array1,$array2,$array3));

    结果回报

    {"AEK":{"country":"Greece","shtv":"MIL"},
     "BER":{"country":"Germany","shtv":"BKE"},
      "CAR":{"country":"Italy","shtv":"KUN"},
      "DAR":{"country":"Turkey","shtv":"DDR"}
    }
    
        3
  •  -1
  •   Rubin Porwal    6 年前
    $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);