-5

I created a JSON variable and parsing it to JSON. But I am not able to read the values. I tried everything. But its not working. Here is my JavaScript code.

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"}}]}';

For your convenience, I'll put the data in a readable format.

    {
   "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"}"
        }
    ]
}

Now I am creating a variable which will store the parsed data. Then I am checking whether it stored or not. So i am reading the first value and alerting it.

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

But when i open this JSP page, I am not able to get see that alert. What is the mistake here?

Update: Sorry I forgot to add the first key when I was copying. Please check it

4

5 回答 5

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"}}]';

之后一切都会好的。

于 2013-03-20T07:14:05.133 回答
1

你确定你的 JSON 是有效的吗?

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

它告诉我你的 JSON 有问题。

于 2013-03-20T07:15:23.597 回答
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},
于 2013-03-20T07:14:21.693 回答
0

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

于 2013-03-20T07:15:25.093 回答
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"
            }
        }
    ]
}
于 2013-03-20T07:20:56.977 回答