【问题标题】:Fetching the JSON value [duplicate]获取 JSON 值 [重复]
【发布时间】:2013-04-22 16:48:36
【问题描述】:

我需要在我的 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"
            }
        ]
    }
]

有什么建议吗?

【问题讨论】:

  • 您想要哪些值?选择名称? ID?描述?你能澄清一下吗?
  • 你的问题是什么?向我们展示您尝试过的代码。
  • 我正在尝试像这样在我的 java 脚本中获取值:

标签: javascript json jquery


【解决方案1】:

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

http://jsonselect.org/

这是一个如何使用它的示例:

(在this 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); }

});

【讨论】:

  • 我得到的 json 来自 jsp function getSelection() { var options = ""; $.getJSON('localhost:8080/r3/selection.jsp').done(function(json) { // var data ='http:../r3/selection.jsp'; alert(json.selectionName); // alert(json.subSelections); value.selectionName + ' option>'; $.each(json.subSelections, function(index, value) { options += ''; }); }).fail(function (jqxhr, textStatus, error) { alert('fail : '+error); }); }
【解决方案2】:

JSON 对象易于处理

var JSON = //Your JSON Object

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

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

and so on.

任何数组对象都可以被视为数组。任何映射的对象都可以使用键返回,例如 JSON 对象的字段名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 2019-03-03
    • 2016-12-29
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    相关资源
    最近更新 更多