Javascript 访问字符串化JSON对象

Javascript 访问字符串化JSON对象,javascript,jquery,json,Javascript,Jquery,Json,正如我的other中的另一位用户建议在这里发布另一个关于解释如何访问JSON对象的问题。我使用的代码是: <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script> $(function () { $.getJSON( "https://api.guildwars2.com/v1/wvw/matches.json", functi

正如我的other中的另一位用户建议在这里发布另一个关于解释如何访问JSON对象的问题。我使用的代码是:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function () {
    $.getJSON(
       "https://api.guildwars2.com/v1/wvw/matches.json",
        function (data) {
            $("#reply").html(JSON.stringify(data));
            // or work with the data here, already in object format
        });
});
</script>

我唯一的问题是我不确定如何将数组设置为JSON数据。

$(function() {
    $.getJSON("https://api.guildwars2.com/v1/wvw/matches.json", function(
            data) {
        $("#reply").html(JSON.stringify(data));
        // or work with the data here, already in object format
        var result = [];//for if it has many matches
        for ( var i=0;i<data.wvw_matches.length;i++ ) {
            var obj = data.wvw_matches[i];
            if (obj.red_world_id == 'xxxx' || obj.blue_world_id == 'xxxx'
                    || obj.green_world_id == 'xxxx') {
                result.push(obj.wvw_match_id);
            }
        }
    });
});
$(函数(){
$.getJSON(“https://api.guildwars2.com/v1/wvw/matches.json“,函数(
(数据){
$(“#reply”).html(JSON.stringify(data));
//或者在这里使用已经是对象格式的数据
var result=[];//如果它有许多匹配项
对于(var i=0;i。使用以下代码-

$(function() {
    $.getJSON("https://api.guildwars2.com/v1/wvw/matches.json", function(
            data) {
        $("#reply").html(JSON.stringify(data));
        // or work with the data here, already in object format
        var result = [];//for if it has many matches
        for ( var i=0;i<data.wvw_matches.length;i++ ) {
            var obj = data.wvw_matches[i];
            if (obj.red_world_id == 'xxxx' || obj.blue_world_id == 'xxxx'
                    || obj.green_world_id == 'xxxx') {
                result.push(obj.wvw_match_id);
            }
        }
    });
});
$(函数(){
$.getJSON(“https://api.guildwars2.com/v1/wvw/matches.json“,函数(
(数据){
$(“#reply”).html(JSON.stringify(data));
//或者在这里使用已经是对象格式的数据
var result=[];//如果它有许多匹配项

对于(var i=0;i,不需要字符串化,您可以直接使用JSON对象:

function(data) {
    var filteredArray=$.grep(data.wvw_matches, function (item) {
        return (item.red_world_id == 'xxxx' || item.blue_world_id == 'xxxx' || item.green_world_id == 'xxxx');
    });
}

有关jQuery.grep.

的信息,无需字符串化,您可以直接使用JSON对象:

function(data) {
    var filteredArray=$.grep(data.wvw_matches, function (item) {
        return (item.red_world_id == 'xxxx' || item.blue_world_id == 'xxxx' || item.green_world_id == 'xxxx');
    });
}

有关jQuery.grep.

的信息,您应该附加JSON。此JSON已经是arrayYou在将其转换为字符串之前已经有了一个对象,您可以直接迭代并搜索其中的键和值,那么为什么要将其转换为字符串呢?您应该附加JSON。此JSON已经是arrayYou之前已经有了一个对象你把它变成一个字符串,你可以直接迭代和搜索其中的键和值,那么为什么要把它变成一个字符串呢?你不应该用
for…in
来循环数组。而且,它应该是
数据。wvw_匹配
,而不仅仅是
数据(var i=0;i
而不是
for…in
从异步请求返回值将毫无用处。您不应该使用
for…in
在数组上循环。此外,它应该是
data.wvw_matches
而不仅仅是
data
。您应该使用
for(var i=0;i
而不是从异步请求返回值的
中的。