如何在php codeigniter中读取json数据

时间:2016-10-01 11:16:54

标签: php json ajax codeigniter

以下是我使用ajax发送到php控制器的json数据

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

我如何读取值并将其存储在php变量中或使用上面的json字符串循环

2 个答案:

答案 0 :(得分:0)

您需要使用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
}

答案 1 :(得分:0)

尝试使用内置函数json_decode

的PHP

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

然后像数组一样访问它