我需要一种非常非常快速的检查字符串是否为JSON的方法.我觉得这不是最好的方法:
function isJson($string) {
return ((is_string($string) &&
(is_object(json_decode($string)) ||
is_array(json_decode($string))))) ? true : false;
}
Run Code Online (Sandbox Code Playgroud)
任何表演爱好者都想改进这种方法吗?
这是一个一般参考问题和答案,涵盖了许多永无止境的"如何访问我的JSON中的数据?" 的问题.它可以处理在PHP中解码JSON和访问结果的广泛基础知识.
我有JSON:
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}
Run Code Online (Sandbox Code Playgroud)
如何在PHP中解码并访问结果数据?
我有一个返回JSON对象的URL,如下所示:
{
"expires_in":5180976,
"access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"
}
Run Code Online (Sandbox Code Playgroud)
我想得到access_token
价值.那我怎么能通过PHP检索呢?
我正在尝试使用PHP从以下JSON文件中获取数据.我特别想要"temperatureMin"和"temperatureMax".
这可能很简单,但我不知道该怎么做.我坚持在file_get_contents("file.json")之后做什么.一些帮助将不胜感激!
{
"daily": {
"summary": "No precipitation for the week; temperatures rising to 6° on Tuesday.",
"icon": "clear-day",
"data": [
{
"time": 1383458400,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1383491266,
"sunsetTime": 1383523844,
"temperatureMin": -3.46,
"temperatureMinTime": 1383544800,
"temperatureMax": -1.12,
"temperatureMaxTime": 1383458400,
}
]
}
}
Run Code Online (Sandbox Code Playgroud) 使用PHP5(cgi)从文件系统输出模板文件,并且有问题吐出原始HTML.
private function fetch($name) {
$path = $this->j->config['template_path'] . $name . '.html';
if (!file_exists($path)) {
dbgerror('Could not find the template "' . $name . '" in ' . $path);
}
$f = fopen($path, 'r');
$t = fread($f, filesize($path));
fclose($f);
if (substr($t, 0, 3) == b'\xef\xbb\xbf') {
$t = substr($t, 3);
}
return $t;
}
Run Code Online (Sandbox Code Playgroud)
即使我已经添加了BOM修复程序,我仍然遇到Firefox接受它的问题.你可以在这里看到一个实时的副本:http://ircb.in/jisti/(如果你想查看它,我在http://ircb.in/jisti/home.html投掷的模板文件)
知道如何解决这个问题吗?O_O
我正在尝试通过json_decode()解析时处理错误的json数据.我正在使用以下脚本:
if(!json_decode($_POST)) {
echo "bad json data!";
exit;
}
Run Code Online (Sandbox Code Playgroud)
如果$ _POST等于:
'{ bar: "baz" }'
Run Code Online (Sandbox Code Playgroud)
然后json_decode处理错误并且吐出"坏的json数据!"; 但是,如果我将$ _POST设置为"无效数据"之类的东西,它会给我:
Warning: json_decode() expects parameter 1 to be string, array given in C:\server\www\myserver.dev\public_html\rivrUI\public_home\index.php on line 6
bad json data!
Run Code Online (Sandbox Code Playgroud)
我是否需要编写自定义脚本来检测有效的json数据,还是有其他一些很好的方法来检测它?
我有一个变量$ v,可以是单个字符串或字符串数组,
我有一个代码:
$a = array();
if (is_array($v)) {
$a = $v;
} else {
$a[] = $v;
}
Run Code Online (Sandbox Code Playgroud)
如何以更优雅的方式完成?(换句话说,如何将变量转换为数组)
我有一个PHP脚本,它通过使用json文件访问file_get_contents()
json文件,我们已经声明了一个php变量.请让我知道有没有办法解析json文件中的php变量.
下面是代码:test.json
{
"result":"$result",
"count":3
}
Run Code Online (Sandbox Code Playgroud)
用于访问json文件的php脚本
<?php
$result = 'Hello';
$event = file_get_contents('test.json');
echo $event;
?>
Run Code Online (Sandbox Code Playgroud)
现在输出如下:
{ "result":"$result", "count":3 }
Run Code Online (Sandbox Code Playgroud)
但我需要像这样的输出
{ "result":"Hello", "count":3 }
Run Code Online (Sandbox Code Playgroud)
我无法访问$result
json文件中的变量.任何帮助Appreciated.Thanks
如何使用PHP从JSON对象获取数据?:
[
{
"name": "Anders",
},
{
"name": "Chris",
},
{
"name": "Peter",
}
]
Run Code Online (Sandbox Code Playgroud)
这是我的PHP代码:
$url = 'http://urltotheapi';
$content = file_get_contents($url);
$json = json_decode($content, true);
Run Code Online (Sandbox Code Playgroud) 我有一个刮刀正在从别处收集一些我无法控制的数据.源数据执行各种有趣的Unicode字符,但它将它们转换为非常无用的格式,所以
\u00e4
Run Code Online (Sandbox Code Playgroud)
对于一个小的'a'与变音符号(没有我认为应该在那里的双引号)*.当然这会在我的HTML中呈现为纯文本.
是否有任何现实的方法将unicode源转换为适当的字符,不涉及我手动处理每个字符串序列并在刮擦期间替换它们?
*这是它吐出的json样本:
({"content":{"pagelet_tab_content":"<div class=\"post_user\">Latest post by <span>D\u00e4vid<\/span><\/div>\n})
Run Code Online (Sandbox Code Playgroud)