如何使用 PHP 修改 Google 地图的大型 json 结构
How to modify large json structure for Google Map using PHP
我有一个 Google 地图,使用 Google 地图 API 从 .json 文件中绘制折线。文件结构的一部分可以在下面看到,它只是为每个折线特征重复。它是在 GIS 中创建并转换为 .geojson 然后 json.
我希望允许用户通过映射界面修改功能的特定属性。我已经构建了界面和用户交互,但在修改 .json 文件时遇到问题。
我试过以下方法:
PHP(注意:我将一些值硬编码为 test/get 它可以工作 )
$jsonString = file_get_contents('../json/hartford.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['UID'] == 25301) {
$data[$key]['RENDER_CL'] = "99";
}
}
$newJsonString = json_encode($data);
file_put_contents('../json/hartford.json', $newJsonString);
错误:
警告:非法字符串偏移 'UID' 在线...
注意:未定义索引:在线UID...
想法:
我觉得我对 json "tree" 的了解还不够深入,无法访问属性。我之前没有修改过 json 数据,所以我有点迷茫。我应该以某种方式更深入地研究结构吗?每个项目的 UID 都是唯一的。谢谢。
JSON 文件
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25301
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.70621818192563523552962578833103179931640625, 41.7494854544135023388662375509738922119140625], [-72.7059645455619971698979497887194156646728515625, 41.74953636350443275659927166998386383056640625], [-72.7057163637438179648597724735736846923828125, 41.749591818049879066165885888040065765380859375], [-72.705553636471080380943021737039089202880859375, 41.749633636231692435103468596935272216796875], [-72.7053663637438063460649573244154453277587890625, 41.749688181686252619329025037586688995361328125], [-72.705190909198364579424378462135791778564453125, 41.749742727140784381845151074230670928955078125], [-72.70496727283472182534751482307910919189453125, 41.749821818049866806177305988967418670654296875], [-72.704716363743813190012588165700435638427734375, 41.749924545322613766984432004392147064208984375], [-72.70461181828926555681391619145870208740234375, 41.74996818168625623002299107611179351806640625]]]
}
}, {
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
},
您正在错误地访问对象的 属性。正确的方法是 $entry['properties']['UID']
.
例子
<?php
$json =
'{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
}';
$obj = json_decode($json, true);
print $obj['properties']['UID'];
我有一个 Google 地图,使用 Google 地图 API 从 .json 文件中绘制折线。文件结构的一部分可以在下面看到,它只是为每个折线特征重复。它是在 GIS 中创建并转换为 .geojson 然后 json.
我希望允许用户通过映射界面修改功能的特定属性。我已经构建了界面和用户交互,但在修改 .json 文件时遇到问题。
我试过以下方法:
PHP(注意:我将一些值硬编码为 test/get 它可以工作 )
$jsonString = file_get_contents('../json/hartford.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['UID'] == 25301) {
$data[$key]['RENDER_CL'] = "99";
}
}
$newJsonString = json_encode($data);
file_put_contents('../json/hartford.json', $newJsonString);
错误:
警告:非法字符串偏移 'UID' 在线...
注意:未定义索引:在线UID...
想法:
我觉得我对 json "tree" 的了解还不够深入,无法访问属性。我之前没有修改过 json 数据,所以我有点迷茫。我应该以某种方式更深入地研究结构吗?每个项目的 UID 都是唯一的。谢谢。
JSON 文件
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25301
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.70621818192563523552962578833103179931640625, 41.7494854544135023388662375509738922119140625], [-72.7059645455619971698979497887194156646728515625, 41.74953636350443275659927166998386383056640625], [-72.7057163637438179648597724735736846923828125, 41.749591818049879066165885888040065765380859375], [-72.705553636471080380943021737039089202880859375, 41.749633636231692435103468596935272216796875], [-72.7053663637438063460649573244154453277587890625, 41.749688181686252619329025037586688995361328125], [-72.705190909198364579424378462135791778564453125, 41.749742727140784381845151074230670928955078125], [-72.70496727283472182534751482307910919189453125, 41.749821818049866806177305988967418670654296875], [-72.704716363743813190012588165700435638427734375, 41.749924545322613766984432004392147064208984375], [-72.70461181828926555681391619145870208740234375, 41.74996818168625623002299107611179351806640625]]]
}
}, {
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
},
您正在错误地访问对象的 属性。正确的方法是 $entry['properties']['UID']
.
例子
<?php
$json =
'{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
}';
$obj = json_decode($json, true);
print $obj['properties']['UID'];