-5

我需要在我的 java 脚本中从此 JSON 中获取值:

[{
        "selectionName": "Select",
        "subSelections": [{
                "id": 4,
                "subSelectionName": "Select",
                "description": "Deepmala"
            }
        ]
    }, {
        "selectionName": "week14",
        "subSelections": [{
                "id": 7,
                "subSelectionName": "1",
                "description": ""
            }
        ]
    }, {
        "selectionName": "test",
        "subSelections": [{
                "id": 6,
                "subSelectionName": "test",
                "description": ""
            }
        ]
    }, {
        "selectionName": "select",
        "subSelections": [{
                "id": 3,
                "subSelectionName": "sub-select",
                "description": "Created by Prakash"
            }
        ]
    }, {
        "selectionName": "testcreate",
        "subSelections": [{
                "id": 1,
                "subSelectionName": "testcreate",
                "description": ""
            }
        ]
    }, {
        "selectionName": "by htmlwidget",
        "subSelections": [{
                "id": 5,
                "subSelectionName": "by htmlwidget",
                "description": "created by html widget"
            }
        ]
    }
]

有什么建议么?

4

2 回答 2

1

您可以使用 JSONSelect 之类的东西来提取某些值。

http://jsonselect.org/

以下是如何使用它的示例:

(在这个 JSFiddle中找到)

$(function(){
/*
Json as easy as SQL ??? RT @lloydhilaiel JSONSelect - CSS-like selectors for JSON - http://jsonselect.org
Testing...
*/
var jsonData = {
    "name": {
        "first": "Lloyd",
        "last": "Hilaiel"
    },
    "favoriteColor": "yellow",
    "languagesSpoken": [
        {
        "language": "Bulgarian",
        "level": 2},
    {
        "language": "English",
        "level": 1},
    {
        "language": "Spanish",
        "level": 7}
    ]
};

var selector = '.name > *'; // xPath CSS like selector

try {

    var resultObj = JSONSelect.match(selector, jsonData);
    console.log(typeof resultObj);
    console.log(resultObj);
    console.log('- - - - -');

    JSONSelect.forEach(selector, jsonData, function(resultObj) {
        console.log(typeof resultObj);
        console.log(resultObj);
        console.log('- - - - -');
        $('body').append('<p>' + $.trim(JSON.stringify(resultObj, null, ' ')) + '</p>');
    });

} catch(e) { console.log(e); }

});
于 2013-04-22T16:59:07.937 回答
0

JSON 对象易于处理

var JSON = //Your JSON Object

JSON[0].selectName //returns 'Select'

JSON[0].subSelections[0].id //returns 4

and so on.

任何数组对象都可以视为数组。任何映射的对象都可以通过使用 JSON 对象的字段名称等键来返回。

于 2013-04-22T17:03:45.827 回答