jointjs:我想得到源的价值":{" id":来自json text

时间:2015-05-22 13:49:17

标签: javascript jquery json

我想在"中获取文字的价值。来源":{" id":来自此JSon文本的JSON:

{
    "cells":
    [
        {
            "type": "devs.Model", "size": { "width": 40, "height": 40 },
            "inPorts": [""], "outPorts": [""], "position": { "x": 103, "y": 345 },
            "angle": 0, "id": "4a8edbca-dd9d-4164-bf0a-fc4cbffdca86", "z": 1,
            "attrs": {
                ".label": { "text": "aa", "ref-x": 0.4, "ref-y": 0.2 },
                "rect": { "fill": "#2ECC71" },
                ".inPorts circle": { "fill": "#16A085", "magnet": "active", "type": "input" },
                ".outPorts circle": { "fill": "#E74C3C", "type": "output" },
                ".inPorts>.port0>.port-label": { "text": "" },
                ".inPorts>.port0>.port-body": { "port": { "id": "in8", "type": "in" } },
                ".inPorts>.port0": { "ref": ".body", "ref-y": 0.5 },
                ".outPorts>.port0>.port-label": { "text": "" },
                ".outPorts>.port0>.port-body": {
                    "port": { "id": "out9", "type": "out" }
                },
                ".outPorts>.port0": { "ref": ".body", "ref-y": 0.5, "ref-dx": 0 }
            }
        },
        {
            "type": "link", "source": {}, "target": {}, "id": "35173392-8b69-44fc-b6f4-f7c8a62319bb", "z": 2, "attrs": {}
        },
        {
            "type": "devs.Model", "size": { "width": 40, "height": 40 },
            "inPorts": [""], "outPorts": [""], "position": { "x": 603, "y": 488 },
            "angle": 0, "id": "39e8bc7f-0553-4c5a-b198-b948b0905ae7", "z": 3,
            "attrs": {
                ".label": { "text": "aaa", "ref-x": 0.4, "ref-y": 0.2 },
                "rect": { "fill": "#2ECC71" },
                ".inPorts circle": { "fill": "#16A085", "magnet": "active", "type": "input" },
                ".outPorts circle": { "fill": "#E74C3C", "type": "output" },
                ".inPorts>.port0>.port-label": { "text": "" },
                ".inPorts>.port0>.port-body": { "port": { "id": "in15", "type": "in" } },
                ".inPorts>.port0": { "ref": ".body", "ref-y": 0.5 },
                ".outPorts>.port0>.port-label": { "text": "" },
                ".outPorts>.port0>.port-body": { "port": { "id": "out16", "type": "out" } },
                ".outPorts>.port0": { "ref": ".body", "ref-y": 0.5, "ref-dx": 0 }
            }
        },
        {
            "type": "link", "source": { "id": "4a8edbca-dd9d-4164-bf0a-fc4cbffdca86", "selector": "g:nth-child(1) g:nth-child(4) g:nth-child(1) circle:nth-child(1) ", "port": "out9" },
            "target": { "id": "39e8bc7f-0553-4c5a-b198-b948b0905ae7", "selector": "g:nth-child(1) g:nth-child(3) g:nth-child(1) circle:nth-child(1) ", "port": "in15" },
            "id": "19bfe3a0-bb48-4665-8f2b-807c3bc33451", "embeds": "", "z": 4,
            "attrs": { ".marker-target": { "d": "M 10 0 L 0 5 L 10 10 z" } }
        }]
}

我做

var t = JSON.stringify(graph )+"";
var obj = jQuery.parseJSON(t);
alert(obj.cells[3].source['.id']);

但它不起作用

2 个答案:

答案 0 :(得分:0)

您不需要进行字符串化,然后重新解析它。只需访问该字段:

alert(graph.cells[3].source.id);

答案 1 :(得分:0)

.

中删除.id



var obj={"cells":[{"type":"devs.Model","size":{"width":40,"height":40},"inPorts":[""],"outPorts":[""],"position":{"x":103,"y":345},"angle":0,"id":"4a8edbca-dd9d-4164-bf0a-fc4cbffdca86","z":1,"attrs":{".label":{"text":"aa","ref-x":0.4,"ref-y":0.2},"rect":{"fill":"#2ECC71"},".inPorts circle":{"fill":"#16A085","magnet":"active","type":"input"},".outPorts circle":{"fill":"#E74C3C","type":"output"},".inPorts>.port0>.port-label":{"text":""},".inPorts>.port0>.port-body":{"port":{"id":"in8","type":"in"}},".inPorts>.port0":{"ref":".body","ref-y":0.5},".outPorts>.port0>.port-label":{"text":""},".outPorts>.port0>.port-body":{"port":{"id":"out9","type":"out"}},".outPorts>.port0":{"ref":".body","ref-y":0.5,"ref-dx":0}}},{"type":"link","source":{},"target":{},"id":"35173392-8b69-44fc-b6f4-f7c8a62319bb","z":2,"attrs":{}},{"type":"devs.Model","size":{"width":40,"height":40},"inPorts":[""],"outPorts":[""],"position":{"x":603,"y":488},"angle":0,"id":"39e8bc7f-0553-4c5a-b198-b948b0905ae7","z":3,"attrs":{".label":{"text":"aaa","ref-x":0.4,"ref-y":0.2},"rect":{"fill":"#2ECC71"},".inPorts circle":{"fill":"#16A085","magnet":"active","type":"input"},".outPorts circle":{"fill":"#E74C3C","type":"output"},".inPorts>.port0>.port-label":{"text":""},".inPorts>.port0>.port-body":{"port":{"id":"in15","type":"in"}},".inPorts>.port0":{"ref":".body","ref-y":0.5},".outPorts>.port0>.port-label":{"text":""},".outPorts>.port0>.port-body":{"port":{"id":"out16","type":"out"}},".outPorts>.port0":{"ref":".body","ref-y":0.5,"ref-dx":0}}},{"type":"link","source":{"id":"4a8edbca-dd9d-4164-bf0a-fc4cbffdca86","selector":"g:nth-child(1) g:nth-child(4) g:nth-child(1) circle:nth-child(1) ","port":"out9"},"target":{"id":"39e8bc7f-0553-4c5a-b198-b948b0905ae7","selector":"g:nth-child(1) g:nth-child(3) g:nth-child(1) circle:nth-child(1) ","port":"in15"},"id":"19bfe3a0-bb48-4665-8f2b-807c3bc33451","embeds":"","z":4,"attrs":{".marker-target":{"d":"M 10 0 L 0 5 L 10 10 z"}}}]};



alert(obj.cells[3].source['id']);