Javascript 访问JSON数据NativeScript

Javascript 访问JSON数据NativeScript,javascript,json,fetch,nativescript,Javascript,Json,Fetch,Nativescript,我对NativeScript和JSON有点陌生,访问JSON文件中的数据有点困难。现在我只是想把一些数据记录下来 以下是我的视图模型中的代码: var config = require("../../shared/config"); var fetchModule = require("fetch"); var ObservableArray = require("data/observable-array").ObservableArray; function StandingsListVi

我对NativeScript和JSON有点陌生,访问JSON文件中的数据有点困难。现在我只是想把一些数据记录下来

以下是我的视图模型中的代码:

var config = require("../../shared/config");
var fetchModule = require("fetch");
var ObservableArray = require("data/observable-array").ObservableArray;

function StandingsListViewModel(items) {
var viewModel = new ObservableArray(items);

viewModel.load = function() {
    var url = config.apiURI + "getStandings.cfm?weekid=397";
    console.log(url);
    return fetch(url)
    .then(handleErrors)
    .then(function(response) {
        console.log(response.json());
        return response.json();
    })
    .then(function(data) {
        console.log("hit");
        data.Result.forEach(function(standing) {
            console.log(standing.place);
            console.log(standing.username);
        });
    });
};

return viewModel;
}

function handleErrors(response) {
if (!response.ok) {
    console.log(JSON.stringify(response));
    throw Error(response.statusText);
}
return response;
}

module.exports = StandingsListViewModel;
以及我引用的JSON文件:

{
    "hiding": 0,
    "lastupdate": 1474481622,
    "refresh": 600,
    "showmax": 0,
    "showtie": 1,

    "displayColumns" : [
        "Points"
        ,"Wins"
        ,"TieDif"

    ],
    "users" : [

        {
            "memberid" : 910089, 
            "username" : "THE DAILY ROUTINE",
            "last_entry" : "1473446820", 
            "place" : "1",
            "record" : [
            "1.0"
            ,"1"
            ,"10.0"

            ]
        } , 
        {
            "memberid" : 2234158, 
            "username" : "MR. MANAGER",
            "last_entry" : "1473277680", 
            "place" : "2",
            "record" : [
            "1.0"
            ,"1"
            ,"26.0"

            ]
        } 
    ] 
}

我知道这可能是非常基本的,非常感谢您的帮助。

可能发生的情况是您正在获取数据,但您没有记录数据以便查看。你正在做:

console.log(response.json());
试着做

console.dump(response.json());
dump输出json,log记录字符串。因此,要么将json字符串化,要么使用console.dump

如果这不起作用,请尝试设置标题

return fetchModule.fetch(config.apiUrl + "getStandings.cfm?weekid=397", {
    method: "GET",
    headers: {
        "Content-Type": "application/json",
    }
})
.then(handleErrors)
.then(function(response) {
    return response.json();
})
.then(function(data) {
    console.dump(data)
    return data;
});

可能发生的情况是您正在获取数据,但您没有记录它以便查看它。你正在做:

console.log(response.json());
试着做

console.dump(response.json());
dump输出json,log记录字符串。因此,要么将json字符串化,要么使用console.dump

如果这不起作用,请尝试设置标题

return fetchModule.fetch(config.apiUrl + "getStandings.cfm?weekid=397", {
    method: "GET",
    headers: {
        "Content-Type": "application/json",
    }
})
.then(handleErrors)
.then(function(response) {
    return response.json();
})
.then(function(data) {
    console.dump(data)
    return data;
});

我发现错误来自于我对一个已经是json对象的响应调用.json()(TypeError:read)。不过非常感谢您的响应,我以后肯定会使用console.dump()。我发现错误来自于我对已经是json对象的响应调用.json()(TypeError:ready read)。不过非常感谢您的回复,我以后肯定会使用这个console.dump()。