【问题标题】:About using JSON with PHP关于在 PHP 中使用 JSON
【发布时间】:2018-01-20 19:31:19
【问题描述】:

我有一个我无法解决的问题。 JQuery 适用于 getJSON,php 不适用于 foreach。我在 PHP 中遇到的错误:

注意:尝试在第 14 行的 C:\xampp\htdocs\plreserv\index.php 中获取非对象的属性 警告:在第 14 行的 C:\xampp\htdocs\plreserv\index.php 中为 foreach() 提供的参数无效

服务的 JSON 输出:

{
    dataList: [
        {
            Date:'11.08.2017',
            Com:'Spr',
            BlsId:'5',
            Type:'break',
            TypeCheck:'0,5',
            perma:'Normal',
            CommaDetail:'0,,,particle,0,,,7',
            Pom:[
                {
                    Num:'1',
                    status:'1',
                    val:'37349',
                    lang:'tr'
                },
                {
                    Num:'2',
                    status:'1',
                    val:'37350',
                    lang:'ru'
                }
            ]
        }
    ]
}

我的php代码:

$file = file_get_contents(url);
$json = json_decode($file);
foreach($json as $data):
// code
endforeach;

有 18 个这样的页面,但没有一个在 PHP 中工作。这是我从 JSON 查看器得到的错误:

Image attachment

对不起,我的英语不好。谢谢。

【问题讨论】:

  • 显示的不是有效的 JSON。在 jsonlint.com 上验证它。除此之外,您还需要显示不起作用的代码
  • 你使用的PHP是?
  • ^ 这个,你能告诉我们你的 php 代码和 foreach 吗?
  • 欢迎来到 Stack Overflow,请花点时间通过 the welcome tour 了解您在这里的道路(同时也获得您的第一个徽章),阅读如何 create a Minimal, Complete, and Verifiable example 并查看 @987654324 @ 这样您就可以增加获得反馈和有用答案的机会。
  • 不要将代码添加为评论,而是更新您的问题。

标签: php jquery json foreach getjson


【解决方案1】:

这是正确的 json 格式:

[
{
    "Date":"11.08.2017",
    "Com":"Spr",
    "BlsId":"5",
    "Type":"break",
    "TypeCheck":"0,5",
    "perma":"Normal",
    "CommaDetail":"0,,,particle,0,,,7",
    "Pom":[
        {
            "Num":"1",
            "status":"1",
            "val":"37349",
            "lang":"tr"
        },
        {
            "Num":"2",
            "status":"1",
            "val":"37350",
            "lang":"ru"
        }
    ]
}]

json里面的所有东西都必须用双引号括起来(整数除外,你没有)

【讨论】:

  • 非常感谢。但我没有制作这个来源。我买了它,这就是来源:(
  • 我可以知道file_get_contents 返回什么吗?
  • 我使用file_get_content的时候,返回值就是我上面发送的值
猜你喜欢
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多