Silex-app-> json()返回整数数据作为字符串 [英] Silex - app->json() returning integer data as strings

查看:127
本文介绍了Silex-app-> json()返回整数数据作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始与Silex合作,帮助我构建一个宁静的api,该api从MySQL数据库返回数据.以前,在使用php和mysql时,我注意到MySQL会在我的json_encode()函数中将整数作为字符串返回.它将所有我的整数值都用引号引起来.我能够解决此问题的唯一方法是将JSON_NUMERIC_CHECK传递给json_encode函数:

I just started working with Silex to help me build a restful api that returns data from my MySQL database. Previously when working with php and mysql I noticed that MySQL would return integers as strings inside my json_encode() function. It would put quotes around all my integer values. The only way I was able to fix this was to pass JSON_NUMERIC_CHECK into the json_encode function:

return json_encode($array, JSON_NUMERIC_CHECK);

为我需要的东西工作很可爱.现在我正在使用silex,我一直在使用它的内置json函数从我的控制器返回值.我注意到我也遇到了同样的问题,即整数作为字符串返回并带有引号引起来.

Worked lovely for what I needed. Now that I'm using silex I've been using it's built-in json function to return values from my controllers. I noticed I'm having the same problem with the integers getting returned as strings with quotes around them.

$app->get('/suppliers', function () use ($app) {
    $sql = "SELECT * FROM suppliers";
    $suppliers = $app['db']->fetchAll($sql);

    return $app->json(array('suppliers' => $suppliers));
});

供应商有一个supplier_id字段,它是一个整数,但在json数据中作为字符串返回.我尝试将JSON_NUMERIC_CHECK传递给数组后的$app->json()函数,但会得到一个InvalidArguementException.

Suppliers have a supplier_id field that is an integer, yet it is being returned as string in the json data. I tried passing JSON_NUMERIC_CHECK into the $app->json() function after my array but would get an InvalidArguementException.

我确实弄清楚了,而不是使用$app->json()函数,我可以只使用json_encode() php函数,它将起作用.像这样:

I did figure out instead of using the $app->json() function I could just use json_encode() php function and it would work. Like so:

$app->get('/suppliers', function () use ($app) {
    $sql = "SELECT * FROM suppliers";
    $suppliers = $app['db']->fetchAll($sql);

    //return $app->json(array('suppliers' => $suppliers));
    return json_encode(array('suppliers' => $suppliers), JSON_NUMERIC_CHECK);
});

是否有任何理由不使用silex $app->json()函数而不以这种方式执行此操作?有更好的方法吗?

Is there any reason NOT to do it this way instead of using the silex $app->json() function? Is there a better way of doing this?

推荐答案

$app->json(...)返回响应JsonResponse.您可以手动创建这种类型的响应并设置编码选项.

$app->json(...) returns response JsonResponse. You can manually create this type of response and set encoding options.

$app->get('/suppliers', function () use ($app) {
    $sql = "SELECT * FROM suppliers";
    $suppliers = $app['db']->fetchAll($sql);

    $response = new \Symfony\Component\HttpFoundation\JsonResponse();
    $response->setEncodingOptions(JSON_NUMERIC_CHECK);
    $response->setData(array('suppliers' => $suppliers));

    return $response;
});

或设置所有就绪的编码内容

or set allready encoded content

$app->get('/suppliers', function () use ($app) {
    $sql = "SELECT * FROM suppliers";
    $suppliers = $app['db']->fetchAll($sql);

    $response = new \Symfony\Component\HttpFoundation\JsonResponse();
    $response->setContent(json_encode(array('suppliers' => $suppliers), JSON_NUMERIC_CHECK));

    return $response;
});

这篇关于Silex-app-> json()返回整数数据作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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