php 在 Codeigniter 中获取 JSON 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19027205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:41:05  来源:igfitidea点击:

Get JSON response In Codeigniter

phpjsoncodeigniter

提问by Jignesh.Raj

I am passing the Object from fiddler to the service written in codeigniter. My Object is something look like this :

我将对象从 fiddler 传递给用 codeigniter 编写的服务。我的对象是这样的:

My Response :

我的回复 :

   {
   "GetResponse":{
      "OrgName":"adfasd",
      "OrgAdr1":"asdf",
      "OrgAdr2":"NA",
      "ProductList":[
          {
               "ProductID":8724,
               "Pallets":0,
               "Pieces":0,
               "Description":"sdfsd"
          }
       ]
   }
}

What I want :

我想要的是 :

I want to save the response as a JSON Object in codeigniter and also want to fetch the JSON Object or Array inside the main Object.

我想在 codeigniter 中将响应保存为 JSON 对象,并且还想在主对象中获取 JSON 对象或数组。

What I tried :

我试过的:

My service method in codeigniter is something like this :

我在 codeigniter 中的服务方法是这样的:

public function Save()
{
     $Data = json_decode(file_get_contents('php://input'));
     echo $Data;
}

But I am getting nothing in Response Bodyinside Fiddler.

但是我在 Fiddler中的Response Body 中什么也没得到。

If I use this code :

如果我使用此代码:

$Data = file_get_contents('php://input');
echo $Data;

then it is showing me the response but in the form of String. I want to save it as an JSON Object.

然后它以字符串的形式向我显示响应。我想将它保存为一个 JSON 对象。

Can anyone tell me what am I missing here ?

谁能告诉我我在这里错过了什么?

回答by Kvadiyatar

use this code:

使用此代码:

header('Content-type: application/json');

$Data = json_decode(file_get_contents('php://input'),true);

now, you will get $Dataas a array.

现在,您将获得$Data一个数组。

and you get value by $Data['name'].

然后你得到价值$Data['name']

回答by Sakibul Alam

json_decode()parses a json string to a php variable in the form of an object or associative array. json_encode()does the reverse.

json_decode()将 json 字符串解析为对象或关联数组形式的 php 变量。json_encode()反之。

I think what is happening is that php://inputis already in json format. When you run it through json_decode()you turn it into a php object which when echoed should throw an error like

我认为正在发生的事情php://input已经是 json 格式。当你运行它时,json_decode()你把它变成一个 php 对象,当echoed 应该抛出一个错误时

Object of class stdClass could not be converted to string ....

类 stdClass 的对象无法转换为字符串 ....

If error reporting is suppressed the script stopped there and you get nothing echoed.

如果错误报告被抑制,脚本就会停在那里,你不会得到任何回应。

回答by Rajeev Ranjan

json_decode()return either an object or an array ,so your save() should be like this

json_decode()返回一个对象或一个数组,所以你的 save() 应该是这样的

public function Save()
{
     $Data = json_decode(file_get_contents('php://input'));
     echo '<pre>';
     print_r( $Data);
}

output will be :-

输出将是:-

stdClass Object
(
    [GetResponse] => stdClass Object
        (
            [OrgName] => adfasd
            [OrgAdr1] => asdf
            [OrgAdr2] => NA
            [ProductList] => Array
                (
                    [0] => stdClass Object
                        (
                            [ProductID] => 8724
                            [Pallets] => 0
                            [Pieces] => 0
                            [Description] => sdfsd
                        )

                )

        )

)

回答by Sherin Jose

Try this,

尝试这个,

public function Save()
{
     $Data = json_decode(file_get_contents('php://input'));
     var_dump($Data);
}

回答by Rohit Kumar Choudhary

To encode any value into JSON we must use json_encode(). what mistake you have done that you are decoding a non json object. So the result in null.

要将任何值编码为 JSON,我们必须使用json_encode(). 您在解码非 json 对象时犯了什么错误。所以结果为空。

 public function Save()
    {
         $Data = json_encode(file_get_contents('php://input'));
         echo $Data;
    }