json多数组语法

时间:2014-09-23 06:42:42

标签: php json

我为json创建了这个带有语法的多数组条件的数组:

$row_set = array(
    "err" => "",
    "msg" => "",
    "data" => array(
        "f" => "",
        "hotel"=> array(
            "att" => "",
            "name" => "name",
            "city" => "",
            "country" => ""
        ),
        "city" => array(
            "att" => "",
            "name" => "",
            "region" => "",
            "country" => "",
            "nr_hotels" => ""
        )
    )
);

echo json_encode($row_set);

但是当我在jsonlint.com上测试它时出现错误:

Parse error on line 1:
array("err"=>"","ms
^
Expecting '{', '['

请帮帮我。我的语法错误在哪里?

2 个答案:

答案 0 :(得分:0)

您的代码生成:

{"err":"","msg":"","data":{"f":"","hotel":{"att":"","name":"name","city":"\r\n","country":""},"city":{"att":"","name":"","region":"","country":"","nr_hotels":""}}}

这是完全有效的JSON。

您正在解析PHP代码,而不是验证程序中的JSON。

答案 1 :(得分:0)

您的代码在jsonlint

上运行完美
$row_set =array(
        "err"=>"",
        "msg"=>"",
        "data"=>array(
                "f"=>"",
                "hotel"=>array(
                        "att"=>"",
                        "name"=>"name",
                        "city"=>"",
                        "country"=>""
                        ),
                "city"=>array(
                        "att"=>"",
                        "name"=>"",
                        "region"=>"",
                        "country"=>"",
                        "nr_hotels"=>""
                        )
        ));
echo json_encode($row_set);

<强>输出

{"err":"","msg":"","data":{"f":"","hotel":{"att":"","name":"name","city":"","country":""},"city":{"att":"","name":"","region":"","country":"","nr_hotels":""}}}

您必须在json_encode上复制jsonlint输出,并且您尝试复制错误的php数组。

检查 Output