javascript - 使用子数组在 php(或 javascript)中读取 Json 文件

标签 javascript php json

这是我的 json 文件 maps.json

[{
"type": "FeatureCollection",
"name": "maps",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": 
    { "IDLo": "1", "SoHieu": "1-1", "KyHieu": "C", "TenLo": "C1-1", "TenCty": "CMC Data Center"}, 
    "geometry": { "type": "Polygon", "coordinates": [ [ [ 106.791800519464871, 10.854928689692501 ], 
                                                        [ 106.792069337729856, 10.855930098557222 ], 
                                                        [ 106.792653322236532, 10.855766231881775 ], 
                                                        [ 106.79231961680415, 10.854783029941672 ], 
                                                        [ 106.791800519464871, 10.854928689692501 ] ] ] } },

{ "type": "Feature", "properties": 
    { "IDLo": "2", "SoHieu": "1-2", "KyHieu": "C", "TenLo": "C1-2", "TenCty": "ASCENDAS" }, 
    "geometry": { "type": "Polygon", "coordinates": [ [ [ 106.79264868743887, 10.855779887550094 ], 
                                                        [ 106.792064702932166, 10.855943754285464 ], 
                                                        [ 106.791786615071828, 10.854942345054598 ], 
                                                        [ 106.79101723865827, 10.855151730898562 ], 
                                                        [ 106.790461062937595, 10.855306494254153 ], 
                                                        [ 106.789969774384346, 10.855424842648457 ], 
                                                        [ 106.789478485831097, 10.855688850436046 ], 
                                                        [ 106.78819928167357, 10.857819111634392 ], 
                                                        [ 106.790915273109462, 10.859412245764197 ], 
                                                        [ 106.791907119811313, 10.857746282442497 ], 
                                                        [ 106.792111050908886, 10.857518691103408 ], 
                                                        [ 106.792379869173871, 10.857291099590915 ], 
                                                        [ 106.792583800271444, 10.856999782201919 ], 
                                                        [ 106.792732113796944, 10.856544598212894 ], 
                                                        [ 106.792741383392297, 10.856116724630859 ], 
                                                        [ 106.79264868743887, 10.855779887550094 ] ] ] } }

]}]

直到 "TenCty": "CMC Data Center" 之前我都不知道如何读取它,以及如何将 "CMC Data Center"替换为其他内容并将其保存在 中maps.json 文件。

当用户提交表单时,信息会保存在MySqli 中,也会保存在json 文件中吗?是否可以?在 javascript 或 PHP 中都可以。

这个json文件是要在Leaflet中显示的 map 信息,所以我必须另存为json。有什么建议吗?提前致谢

最佳答案

您需要完成几个步骤。

首先获取json文件的内容:

$contents = file_get_contents('maps.json');

然后应用json_decode()并转换为数组:

$contents_decoded = json_decode($contents, true);

现在你可以获得"TenCty": "CMC Data Center":

echo $contents_decoded[0]['features'][0]['properties']['TenCty']; //CMC Data Center

您可以替换值:

$contents_decoded[0]['features'][0]['properties']['TenCty'] = 'something else';

现在您可以对新数组进行编码:

$contents = json_encode($contents_decoded);
echo $contents;
Output:
[{"type":"FeatureCollection","name":"maps","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features":[{"type":"Feature","properties":{"IDLo":"1","SoHieu":"1-1","KyHieu":"C","TenLo":"C1-1","TenCty":"something else"},"geometry":{"type":"Polygon","coordinates":[[[106.79180051946487,10.854928689692501],[106.79206933772986,10.855930098557222],[106.79265332223653,10.855766231881775],[106.79231961680415,10.854783029941672],[106.79180051946487,10.854928689692501]]]}},{"type":"Feature","properties":{"IDLo":"2","SoHieu":"1-2","KyHieu":"C","TenLo":"C1-2","TenCty":"ASCENDAS"},"geometry":{"type":"Polygon","coordinates":[[[106.79264868743887,10.855779887550094],[106.79206470293217,10.855943754285464],[106.79178661507183,10.854942345054598],[106.79101723865827,10.855151730898562],[106.7904610629376,10.855306494254153],[106.78996977438435,10.855424842648457],[106.7894784858311,10.855688850436046],[106.78819928167357,10.857819111634392],[106.79091527310946,10.859412245764197],[106.79190711981131,10.857746282442497],[106.79211105090889,10.857518691103408],[106.79237986917387,10.857291099590915],[106.79258380027144,10.85699978220192],[106.79273211379694,10.856544598212894],[106.7927413833923,10.856116724630859],[106.79264868743887,10.855779887550094]]]}}]}]

最后一步是您可以将新内容放回 maps.json 文件:

file_put_contents('maps.json', $contents);

关于javascript - 使用子数组在 php(或 javascript)中读取 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51645532/

相关文章:

php - Eloquent - $fillable 的真正用途是什么?

ios - 在 Background Swift 中循环并将 json 数据附加到 UIImage[]

用于删除 iFrame 中加载的元素的 Javascript

javascript - ReactJS 将元素拥有的 prop 传递给函数

php - 使用php从数据库显示pdf

php - Laravel - 自定义 .env 文件

arrays - 如何使用 JSON_MODIFY 更新数组中符合某些条件的所有元素

json - 缩放 websocket Node 服务器

javascript - Ajax jquery 成功范围

javascript - 如何使用 require.js 加载 jQuery with noConflict