【问题标题】:how to insert a new field in a json file如何在json文件中插入新字段
【发布时间】:2018-01-19 05:57:09
【问题描述】:

我有一个这样的 JSON 文件,

{
    "AAL": {
        "id": "32313",
        "airport_name": "Aalborg",
        "latitude": "57.1",
        "longitude": "9.85",
        "timezone": "2",
        "dst_indicator": "E",
        "city": "Aalborg",
        "country": "Denmark",
        "country_code": "DK",
        "region": "TC1",
        "listing_display": "true",
        "pseudonyms": ""
    },
    "AAR": {
        "id": "32314",
        "airport_name": "Tirstrup",
        "latitude": "56.15",
        "longitude": "10.2167",
        "timezone": "2",
        "dst_indicator": "E",
        "city": "Aarhus",
        "country": "Denmark",
        "country_code": "DK",
        "region": "TC1",
        "listing_display": "true",
        "pseudonyms": ""
    }
}

我需要向它添加一个新字段并使用 php.ini 获得这样的结果。任何帮助将不胜感激。

{
    "AAL": {
        "station_code":"AAL",
        "id": "32313",
        "airport_name": "Aalborg",
        "latitude": "57.1",
        "longitude": "9.85",
        "timezone": "2",
        "dst_indicator": "E",
        "city": "Aalborg",
        "country": "Denmark",
        "country_code": "DK",
        "region": "TC1",
        "listing_display": "true",
        "pseudonyms": ""
    },
    "AAR": {
        "station_code":"AAR",
        "id": "32314",
        "airport_name": "Tirstrup",
        "latitude": "56.15",
        "longitude": "10.2167",
        "timezone": "2",
        "dst_indicator": "E",
        "city": "Aarhus",
        "country": "Denmark",
        "country_code": "DK",
        "region": "TC1",
        "listing_display": "true",
        "pseudonyms": ""
    }
}

【问题讨论】:

标签: php json


【解决方案1】:

这样就可以实现输出了..

$data= '{
    "AAL": {
        "id": "32313",
        "airport_name": "Aalborg",
        "latitude": "57.1",
        "longitude": "9.85",
        "timezone": "2",
        "dst_indicator": "E",
        "city": "Aalborg",
        "country": "Denmark",
        "country_code": "DK",
        "region": "TC1",
        "listing_display": "true",
        "pseudonyms": ""
    },
    "AAR": {
        "id": "32314",
        "airport_name": "Tirstrup",
        "latitude": "56.15",
        "longitude": "10.2167",
        "timezone": "2",
        "dst_indicator": "E",
        "city": "Aarhus",
        "country": "Denmark",
        "country_code": "DK",
        "region": "TC1",
        "listing_display": "true",
        "pseudonyms": ""
    }
}';

$arrData  =  json_decode($data,true);
foreach($arrData as $key=>$val){
    $arrData[$key]['station_code']=$key;
}

echo json_encode($arrData);
exit;

【讨论】:

    【解决方案2】:

    您应该将 json 解析为 php 变量。在这里添加额外的字段并将其解析回 json。

    Check out the php documentation

    【讨论】:

      【解决方案3】:

      PHP 中的 JSON 解析

      读取包含 JSON 数据的文件:

      $json_array_assc = json_decode(file_get_contents($file_name), true);
      

      要将字段添加到数组中,您可以这样做:

      $json_array_assc["NEWDATA"]=array('newdata1'=>'foo1','newdata2'=>'foo2');
      

      要将新数据写入文件,请使用:

      file_put_contents($file_name, json_encode($json_array_assc));
      

      或者直接在控制台窗口打印数组:

      echo "<pre>";
      print_r($json_array_assc);
      

      echo json_encode($json_array_assc);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-16
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 2023-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多