[英]Read JSON Data Using PHP
Solr 以以下 JSON 格式返回響應。
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"indent":"on",
"start":"0",
"q":"*:*",
"wt":"json",
"version":"2.2",
"rows":"10"}},
"response":{"numFound":3,"start":0,"docs":[
{
"student_id":"AB1001",
"student_name":[
"John"]
},
{
"student_id":"AB1002",
"student_name":[
"Joe"]
},
{
"student_id":"AB1003",
"student_name":[
"Lorem"]
}]
}}
使用 PHP 讀取 student_id、student_name 的簡單方法是什么?
使用$obj = json_decode($yourJSONString);
將其轉換為 object。
然后使用foreach($obj->response->docs as $doc)
迭代“docs”。
然后,您可以使用$doc->student_id
和$doc->student_name[0]
訪問這些字段。
PHP 有一個 json_decode function 允許您將 JSON 字符串轉換為數組:
$array = json_decode($json_string, true);
$student_id = $array['response']['docs'][0]['student_id'];
...
當然,您可能想要遍歷學生列表而不是訪問索引 0。
$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...
$json_a = json_decode($string, TRUE);
$json_o = json_decode($string);
#array method
foreach($json_a['response']['docs'] as $students)
{
echo $students['student_id']." name is ".$students['student_name'][0];
echo "<br>";
}
#Object Method`enter code here`
foreach($json_o->response->docs as $sthudent_o)
{
echo $sthudent_o->student_id. " name is ".$sthudent_o->student_name[0];
echo "<br>";
}
為什么不將 PHP 客戶端之一用於 Solr 或 PHP 響應編寫器? 見http://wiki.apache.org/solr/SolPHP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.