邮递员在json响应中显示意外的"A",但json格式正确 [英] Postman show unexpected 'A' in json response , but json is well formed

查看:179
本文介绍了邮递员在json响应中显示意外的"A",但json格式正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Silex框架开发后端,并且正在测试这段代码

I'm developing a backend with Silex framework and i'm testing this piece of code

foreach($P as $key=>$value)
    {
        $strInsert  =$key."=>".$value;      
        array_push($json,$strInsert);                 
    }
    print_r($json);
    return json_encode($json);

在每个调用包含foreach的路由的浏览器上,它都能很好地打印,并且给定的输出被不同的json验证器认为格式正确. 在邮递员中,当我单击漂亮" Json时,它显示Unexpected 'A'.
在raw,html和其他视图选项上,可以毫无问题地打印json文档. 我应该继续为这个问题担心还是忽略它? 如果我不应该忽略它,有没有解决的方法?

On every browser calling the route which contains the foreach it prints well, and the output given is considered well-formed by different json validators. In postman when i click on 'pretty' Json it shows Unexpected 'A'.
On raw,html and other views options the json document is print without problems. Should I keep worrying for this problem or just ignore it? If I shouldn't ignore it, is there a fix ?

推荐答案

在Silex框架上进行了大量工作以构建我的后端之后,多亏了邮递员(chrome应用程序,非常方便用于测试http协议),我发现php会以Json格式等待响应.

After a lot of work on Silex framework to build my backend, Thanks to postman (a chrome app,really handy for testing http protocols),I've figured out that php waits the response in a Json format.

    Example A :
    call route /path/getinfo/ with method get
    execute the code --> result=$app->SomeFunctionThatInterrogatesDB(param1...) **firing error
                         return result;

这是要解决的问题:在 somefunction 中,您对数据库进行查询,并返回一行(或多行,具体取决于查询的类型),PHP认为响应是用JSON结构化的,但不是,因此会引发该错误.
该意外的'A'表示 嘿,我的json在哪里?我什么都不懂!"
此修复程序或解决方法是只是将结果以json格式封装在路由内.

here's the deal : in that somefunction you make a query to the db and it returns a row (or multiple rows depending from the type of query),PHP thinks that the response is structured in JSON, but it is not, so it fires that error.
That unexpected 'A' just means "hey,where is my json? I can't understand nothing !"
The fix or the workaround, is just to encapsulate your result in json format inside the route.

严格来说,将return result更改为return $app->json(result). 这段代码是基于silex的,但是即使您使用的不是silex的其他框架,解决方案也是将最终结果以json格式字符串化. /p>

To be strict change return result with return $app->json(result). This code is based on silex but even if you are using other framework out of silex, the solution is to stringify your final result in json format.Here is a little graph(call me davinci if you want) to understand better the flow.

 |*Frontend*|  route request     |   *Backend*   | query     |*Database*|
 |          |------------------->|               |---------->|          |
 |          |    Response        | json(result)  |  array    |          |
 |          |<-------------------|<--------------|<----------|          |

我希望我的回答对任何人都有帮助.

I hope that my answer can be of help to anyone.

这篇关于邮递员在json响应中显示意外的"A",但json格式正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆