Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取JSON值_Javascript_Json_Jquery - Fatal编程技术网

Javascript 获取JSON值

Javascript 获取JSON值,javascript,json,jquery,Javascript,Json,Jquery,我需要在java脚本中从这个JSON获取值: [{ "selectionName": "Select", "subSelections": [{ "id": 4, "subSelectionName": "Select", "description": "Deepmala" } ] }, { "select

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

有什么建议吗?

您可以使用JSONSelect之类的工具来提取某些值

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

(位于)

$(函数(){
/*
像SQL一样简单的Json???RT@Lloydhiliel JSONSelect-类似CSS的Json选择器-http://jsonselect.org
测试。。。
*/
var jsonData={
“姓名”:{
“第一”:“劳埃德”,
“last”:“Hilaiel”
},
“favoriteColor”:“黄色”,
“语言戳破”:[
{
“语言”:“保加利亚语”,
“级别”:2},
{
“语言”:“英语”,
“级别”:1},
{
“语言”:“西班牙语”,
“级别”:7}
]
};
var选择器='.name>*';//类似xPath CSS的选择器
试一试{
var resultObj=JSONSelect.match(选择器,jsonData);
console.log(结果类型j);
console.log(resultObj);
console.log('-');
forEach(选择器、jsonData、函数(resultObj){
console.log(结果类型j);
console.log(resultObj);
console.log('-');
$('body').append(''+$.trim(JSON.stringify(resultObj,null'))+'

'); }); }catch(e){console.log(e);} });
JSON对象很容易处理

var JSON = //Your JSON Object

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

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

and so on.

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

需要哪些值?选择名字?身份证件描述你能澄清一下吗?你有什么问题?向我们展示您尝试过的代码。我尝试在java脚本中这样获取值:我获取的json来自jsp函数getSelection(){var options=“”;$.getJSON('{//var data='http:../r3/selection.jsp';alert(json.selectionName);//alert(json.subSelections);value.selectionName+';$。每个(json.subSelections,函数(index,value){options+=''+value.description+'';});}).fail(函数(jqxhr,textStatus,error){alert('fail:'+error);});}
var JSON = //Your JSON Object

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

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

and so on.