JSON解码:混合类型 - 无效语法错误

时间:2016-01-01 01:29:02

标签: javascript php arrays json object

这是我的JSON示例

            {"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }
        {"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }
        {"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }

这是php:

<?php
$file = file_get_contents("in.txt");
$json = json_decode($file, true);

/ **跳过json_last_error()代码.. 错误是这样的: JSON_ERROR_SYNTAX ** /

$tasks = $json["tasks"];
foreach ($tasks as $key => $value)
{
    echo "<ul>";
    echo "<li>".$value['id']."</li>";
    echo "<li>".$value['quality']."</li>";
    echo "<li>".$value['position']."</li>";
    echo "<li>".$value['profile']."</li>";
    echo "<li>".$value['type']."</li>";
    echo "<li>".$value['valid']."</li>";
    echo "<li>".$value['member']['carrier']."</li>";
    echo "<li>".$value['member']['zone']."</li>";
    echo "<li>".$value['member']['offd']."</li>";
    echo "<li>".$value['taketime']."</li>";
    echo "<li>".$value['listeners']['count']."</li>";
    echo "<li>".$value['taketime']."</li>";
    echo "<li>".$value['message']."</li>";
    echo "<li>".$value['level']."</li>";
    echo "</ul>";
        }
    ?>

实际上,错误不允许使用JSON执行代码。 但是如果你只使用JSON的一部分,就像这样:

{"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }

然后它工作!

请你,请指导我,我的语法错误在哪里......也许你也可以帮助我理解如何从获取值的每个循环获得 start_poz 值任务数组?

非常感谢新年快乐! ;)

3 个答案:

答案 0 :(得分:3)

您在顶层的输入文件中有三个单独的JSON对象。您需要将整个输入包装为数组[ ... ]或单独解析每个输入。

答案 1 :(得分:1)

jsonlint.com说:

Error: Parse error on line 58:
...z": "36774263743"} { "tasks": [{     "id
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'

所以当它到达那个&#39; {&#39;它处于一个只有

的状态
  • 文件的结尾,
  • 近距离大括号,
  • 逗号,
  • 或关闭括号

是有效的。

答案 2 :(得分:1)

Jim说得对,这是更正后的JSON

{
  "tasklist": [{
    "tasks": [{
      "id": "27604_11",
      "quality": "A4",
      "position": "183567",
      "profile": "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
      "type": "image",
      "valid": "24 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "4minutes",
      "listeners": {
        "count": 0
      },
      "message": "stay prepared",
      "level": "High"
    }, {
      "id": "27604_12",
      "quality": "B3",
      "position": "183569",
      "profile": "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
      "type": "image",
      "valid": "11 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "7minutes",
      "listeners": {
        "count": 2
      },
      "message": "ready",
      "level": "High"
    }, {
      "id": "73754_35",
      "quality": "A3",
      "position": "13542",
      "profile": "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
      "type": "image",
      "valid": "5 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "22minutes",
      "listeners": {
        "count": 0
      },
      "message": "Good",
      "level": "Normale"
    }],
    "start_poz": "36774263743"
  }, {
    "tasks": [{
      "id": "27604_11",
      "quality": "A4",
      "position": "183567",
      "profile": "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
      "type": "image",
      "valid": "24 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "4minutes",
      "listeners": {
        "count": 0
      },
      "message": "stay prepared",
      "level": "High"
    }, {
      "id": "27604_12",
      "quality": "B3",
      "position": "183569",
      "profile": "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
      "type": "image",
      "valid": "11 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "7minutes",
      "listeners": {
        "count": 2
      },
      "message": "ready",
      "level": "High"
    }, {
      "id": "73754_35",
      "quality": "A3",
      "position": "13542",
      "profile": "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
      "type": "image",
      "valid": "5 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "22minutes",
      "listeners": {
        "count": 0
      },
      "message": "Good",
      "level": "Normale"
    }],
    "start_poz": "36774263743"
  }, {
    "tasks": [{
      "id": "27604_11",
      "quality": "A4",
      "position": "183567",
      "profile": "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
      "type": "image",
      "valid": "24 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "4minutes",
      "listeners": {
        "count": 0
      },
      "message": "stay prepared",
      "level": "High"
    }, {
      "id": "27604_12",
      "quality": "B3",
      "position": "183569",
      "profile": "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
      "type": "image",
      "valid": "11 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "7minutes",
      "listeners": {
        "count": 2
      },
      "message": "ready",
      "level": "High"
    }, {
      "id": "73754_35",
      "quality": "A3",
      "position": "13542",
      "profile": "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
      "type": "image",
      "valid": "5 days",
      "member": {
        "carrier": null,
        "zone": null,
        "offd": null
      },
      "taketime": "22minutes",
      "listeners": {
        "count": 0
      },
      "message": "Good",
      "level": "Normale"
    }],
    "start_poz": "36774263743"
  }]
}