简体   繁体   English

如何在php codeigniter中读取json数据

[英]how to read json data in php codeigniter

Following below is the json data which i am sending to php controller by using ajax以下是我使用ajax发送到php控制器的json数据

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

how can i read values and stored it in php variable or use loop with above json string我如何读取值并将其存储在 php 变量中或使用带有上述 json 字符串的循环

You need to decode your json string by using json_decode() than you can use foreach() loop.您需要使用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

And you can read the data as:您可以将数据读取为:

$decoded = json_decode($jsonString,TRUE);

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

Try to use PHP built in function json_decode尝试使用 PHP 内置函数json_decode

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

Then access it like an array然后像数组一样访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM