在Javascript中读取JSON

时间:2013-03-20 07:07:12

标签: javascript json jsp

我创建了一个JSON变量并将其解析为JSON。但我无法阅读这些价值观。我尝试了一切。但它不起作用。这是我的JavaScript代码。

var graph='{"graphSpace":[{"rectangle":{"minX":0.0,"minY":0.0,"maxX":0.0,"maxY":0.0},"contents":"","rights":[],"lefts":[],"tops":[],"downs":[],"annotation":{"id":255,"type":"line"}],[{"rectangle":{"minX":174.0,"minY":87.0,"maxX":989.0,"maxY": 136.0},"contents":"Carter Brothers Fire u0026 Life Safety","rights":[{"id":59}],"lefts":[],"tops":[{"id":255}],"downs":[{"id":14},{"id":16},{"id":19},{"id":21},{"id":28},{"id":31},{"id":33},{"id":35},{"id":42},{"id":45},{"id":47},{"id":49},{"id":52},{"id":183},{"id":169},{"id":199},{"id":190},{"id":201},{"id":192},{"id":208},{"id":215},{"id":230},{"id":233},{"id":240},{"id":243},{"id":246}],"annotation":{"id":7,"type":"line"}},{"rectangle":{"minX":2073.0,"minY":92.0,"maxX":2327.0,"maxY":150.0},"contents":"Invoice","rights":[],"lefts":[{"id":7}],"tops":[{"id":7},{"id":255}],"downs":[{"id":67},{"id":70},{"id":72},{"id":74},{"id":76},{"id":100},{"id":102},{"id":105},{"id":108},{"id":111},{"id":114},{"id":135},{"id":160},{"id":151},{"id":162},{"id":153},{"id":264},{"id":280},{"id":302}],"annotation":{"id":59,"type":"line"}}]}';

为方便起见,我会将数据放入可读格式。

    {
   "graphSpace":
    [
        {"rectangle":
            "{  "minX": 0.0,  "minY": 0.0,  "maxX": 0.0,  "maxY":0.0}",
        "contents":"",
        "rights":"[]",
        "lefts":"[]",
        "tops":"[]",
        "downs":"[]",
        "annotation":
            "{  "id": 255,  "type": "line"}"
    ],
    [
        {"rectangle":
            "{  "minX": 174.0,  "minY": 87.0,  "maxX": 989.0,  "maxY": 136.0}",
        "contents":"JSON u0026 JAVASCRIPT",
        "rights":"
            [{"id":59}]",
        "lefts":"[]",
        "tops":"[{"id":255}]",
        "downs":"
            [{"id":14},]",
        "annotation":
            "{  "id": 7,  "type": "line"}"
        },
        {"rectangle":
            "{  "minX": 2073.0,  "minY": 92.0,  "maxX": 2327.0, "maxY":150.0}",
        "contents":"JSON EXample",
        "rights":"[]",
        "lefts":
            "[{"id":7}]",
        "tops":
            "[{"id":7},{"id":255}]",
        "downs":
            "[{"id":67},{"id":70},{"id":72},]",
        "annotation":
            "{"id": 59,  "type": "line"}"
        }
    ]
}

现在我正在创建一个存储已解析数据的变量。 然后我正在检查它是否存储。所以我正在阅读第一个值并提醒它。

var graphSpace=JSON.parse(graph);
var minx = graphSpace[0].rectangle.minX;
alert(minx);

但是当我打开这个JSP页面时,我无法看到那个警报。这里有什么错误?

更新:抱歉,我忘记在复印时添加第一个密钥。请检查

5 个答案:

答案 0 :(得分:1)

格式无效。删除包裹数组的花括号。

 var graph='[{"rectangle":{"minX":0.0,"minY":0.0,"maxX":0.0,"maxY":0.0},"contents":"","rights":[],"lefts":[],"tops":[],"downs":[],"annotation":{"id":255,"type":"line"}],[{"rectangle":{"minX":174.0,"minY":87.0,"maxX":989.0,"maxY": 136.0},"contents":"Carter Brothers Fire u0026 Life Safety","rights":[{"id":59}],"lefts":[],"tops":[{"id":255}],"downs":[{"id":14},{"id":16},{"id":19},{"id":21},{"id":28},{"id":31},{"id":33},{"id":35},{"id":42},{"id":45},{"id":47},{"id":49},{"id":52},{"id":183},{"id":169},{"id":199},{"id":190},{"id":201},{"id":192},{"id":208},{"id":215},{"id":230},{"id":233},{"id":240},{"id":243},{"id":246}],"annotation":{"id":7,"type":"line"}},{"rectangle":{"minX":2073.0,"minY":92.0,"maxX":2327.0,"maxY":150.0},"contents":"Invoice","rights":[],"lefts":[{"id":7}],"tops":[{"id":7},{"id":255}],"downs":[{"id":67},{"id":70},{"id":72},{"id":74},{"id":76},{"id":100},{"id":102},{"id":105},{"id":108},{"id":111},{"id":114},{"id":135},{"id":160},{"id":151},{"id":162},{"id":153},{"id":264},{"id":280},{"id":302}],"annotation":{"id":59,"type":"line"}}]';

之后一切都会正确。

答案 1 :(得分:1)

你确定你的JSON有效吗?

使用此网站检查: http://jsonlint.com/

它告诉我你的JSON存在问题。

答案 2 :(得分:0)

此格式无效。最外面的{}是不需要的,并删除嵌入对象周围的引号,如:

"rectangle": "{  "minX": 174.0,  "minY": 87.0,  "maxX": 989.0,  "maxY": 136.0}",

相反:

"rectangle": {  "minX": 174.0,  "minY": 87.0,  "maxX": 989.0,  "maxY": 136.0},

答案 3 :(得分:0)

您的JSON无效。 JSON中对象的属性需要列为名称:值对,但您还没有为最外层对象执行此操作。由于您似乎希望这是一个数组,我建议将最外面的括号更改为方括号。

答案 4 :(得分:0)

使用jsonlint.com来破坏你的json.com。经过修正后,它会是这样的。适用于LEVEL 1的名称和适用于该键的LEVEL

{
    "LEVEL 1": [
        {
            "rectangle": {
                "minX": 0,
                "minY": 0,
                "maxX": 0,
                "maxY": 0
            },
            "contents": "",
            "rights": "[]",
            "lefts": "[]",
            "tops": "[]",
            "downs": "[]",
            "annotation": {
                "id": 255,
                "type": "line"
            }
        }
    ],
    "LEVEL 11": [
        {
            "rectangle": {
                "minX": 174,
                "minY": 87,
                "maxX": 989,
                "maxY": 136
            },
            "contents": "JSON u0026 JAVASCRIPT",
            "rights": {
                "id": 59
            },
            "lefts": "[]",
            "tops": {
                "id": 255
            },
            "downs": {
                "id": 14
            },
            "annotation": {
                "id": 7,
                "type": "line"
            }
        },
        {
            "rectangle": {
                "minX": 2073,
                "minY": 92,
                "maxX": 2327,
                "maxY": 150
            },
            "contents": "JSON EXample",
            "rights": "[]",
            "lefts": {
                "id": 7
            },
            "tops": {
                "id": 7
            },
            "you2": {
                "id": 255
            },
            "downs": [
                {
                    "id": 67
                },
                {
                    "id": 70
                },
                {
                    "id": 72
                }
            ],
            "annotation": {
                "id": 59,
                "type": "line"
            }
        }
    ]
}