Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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 使用jquery访问json数据以设置值_Javascript_Jquery_Json_R_Ajax - Fatal编程技术网

Javascript 使用jquery访问json数据以设置值

Javascript 使用jquery访问json数据以设置值,javascript,jquery,json,r,ajax,Javascript,Jquery,Json,R,Ajax,我将数据放入json字符串中,该字符串在下面的代码中作为r.d 现在我想进入它的领域。那么我应该如何访问它呢 这是密码 $.ajax({ url: "GET_DATA_BY_STORE.aspx/GETSTOREINFO", dataType: "json", type: "POST", contentType: 'application/json; charset=utf-8',

我将数据放入json字符串中,该字符串在下面的代码中作为r.d

现在我想进入它的领域。那么我应该如何访问它呢

这是密码

$.ajax({
            url: "GET_DATA_BY_STORE.aspx/GETSTOREINFO",
            dataType: "json",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ STORE_ID: STORE_ID }),
            async: true,
            processData: false,
            cache: false,
            success: function (r) {                   
                alert(getJSONValue[0].STORE_ID);                    
            },
            error: function (xhr) {
               // alert('Error while selecting list..!!');
            }
        })
在r.d中,我得到的数据是


使用将其转换为json对象后,您可以像通常使用javascript对象一样访问属性:


您是否尝试将其转换为json对象?->然后您应该能够使用标准javascript属性访问并读取fields@revy:是的,我试过这样的var jsondata=JSON.parser.d;在jsondata警报中,我得到的是[object object]。那么我如何访问它的字段和值呢?我试着这样做`var jsonString=r.d;var r=JSON.parsejsonString alertr;`但是仍然无法访问字段smine只是一个示例,它向您展示了在json对象中转换原始json之后如何访问javascript中的数据。在本例中,您可以在r.d中显示第一个对象的第一个属性,如下所示:alertr.d[0][RRSOC\u ID]//应该显示18
var jsonString = '{"d": [{"field1": "value1", "field2": 15.0}, {"field3": [1,2,3]}]}'
var r = JSON.parse(jsonString)

console.log(r.d)
// output: [ { field1: 'value1', field2: 15 }, { field3: [ 1, 2, 3 ] } ]

console.log(r.d[0].field1)
// output: value1

console.log(r.d[0].field2)
// output: 15

console.log(r.d[1].field3)
// output: [ 1, 2, 3 ]


// you can also use brackets notation to access properties in object
console.log(r.d[0]["field1"])
// output: value1


// or you can iterate properties if the data type of a field is an array (r.d is an array)
r.d.forEach(function(prop) {
    console.log(prop);
})
// output:  { field1: 'value1', field2: 15 }
//          { field3: [ 1, 2, 3 ] }