我在PHP中使用Web服务,但它返回一个json,我无法访问像CodMateria这样的特定值。你能帮帮我吗? 我试图使用:
$materia->GetResult->Materias->CodMateria;
我无法访问的结果:
string(934) "{"GetResult":{"Materias":[{"CodMateria":"001","Materia":"Math","paralelo":"A"},
{"CodMateria":"002","Materia":"Math2","paralelo":"B"},
{"CodMateria":"003","Materia":"Math3","paralelo":"C"},
{"CodMateria":"004","Materia":"Math4","paralelo":"D"}]}}"
答案 0 :(得分:2)
使用json_decode()
。有多个codeMateria
所以为了访问第一个使用:
$materia->GetResult->Materias[0]->CodMateria
答案 1 :(得分:1)
根据文档,您需要指定是否需要关联数组而不是json_decode中的对象,这将是代码:
json_decode($ jsondata,true);
答案 2 :(得分:0)
根据您的提及,您可以使用json_decode
def get_neighbor():
pt = WKTElement('POINT(%s %s)' % (longitude, latitude), srid=srid)
# Get total count of neighbor with distance less than 1000m for example.
total_count = db.session.query(User, func.ST_Distance(User.geom, pt)). \
filter(func.ST_DWith(User.geom, pt, 1000).count()
if total_count > 0:
result_list = db.session.query(User, func_ST_Distance(User.geom, pt)). \
filter(func.ST_DWithin(User.geom, pt, 1000).all()
return jsonify({'total_count': total_count, 'result_list':result_list})
else
return jsonify({'total_count': total_count})
输出:
<?php
$jsonData = '{"GetResult":{"Materias":[{"CodMateria":"001","Materia":"Math","paralelo":"A"},
{"CodMateria":"002","Materia":"Math2","paralelo":"B"},
{"CodMateria":"003","Materia":"Math3","paralelo":"C"},
{"CodMateria":"004","Materia":"Math4","paralelo":"D"}]}}';
$materia = json_decode($jsonData);
echo $materia->GetResult->Materias[0]->CodMateria;
示例Eval
<强>可替换地,强>
您可以使用001
将您的转换为数组。在这种情况下,您需要像这样访问:
json_decode($jsonData, true);
答案 3 :(得分:0)
尝试使用json_decode
<?php
$strJson = '{"GetResult":{"Materias":[{"CodMateria":"001","Materia":"Math","paralelo":"A"},
{"CodMateria":"002","Materia":"Math2","paralelo":"B"},
{"CodMateria":"003","Materia":"Math3","paralelo":"C"},
{"CodMateria":"004","Materia":"Math4","paralelo":"D"}]}}';
$arrJson = json_decode($strJson);
foreach($arrJson->GetResult->Materias as $objResult)
{
echo "<br>".$objResult->CodMateria;
}
?>
这将给出如下输出:
001
002
003
004
以类似的方式,你也可以访问其他值..!
e.g。
$objResult->Materia;
$objResult->paralelo;