簡體   English   中英

如何在php codeigniter中讀取json數據

[英]how to read json data in php codeigniter

以下是我使用ajax發送到php控制器的json數據

[  
   {  
      "First":"A,b"
   },
   {  
      "Second":""
   },
   {  
      "Third":""
   },
   {  
      "Fourth":""
   },
   {  
      "Fifth":""
   },
   {  
      "Sixth":""
   },
   {  
      "Seventh":""
   },
   {  
      "Eight":""
   },
   {  
      "Ninth":""
   },
   {  
      "Tenth":""
   }
]

我如何讀取值並將其存儲在 php 變量中或使用帶有上述 json 字符串的循環

您需要使用json_decode()來解碼 json 字符串,而不是使用foreach()循環。

print_r(json_decode($jsonString,TRUE)); // will return response in an array

print_r(json_decode($jsonString)); // will return response in an object

您可以將數據讀取為:

$decoded = json_decode($jsonString,TRUE);

foreach( $decoded as $key => $value ){
    // print values
    // print key
}

嘗試使用 PHP 內置函數json_decode

http://www.php.net/manual/en/function.json-decode.php

然后像數組一樣訪問它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM