2016-09-22 3 views
0

NativeScript 및 JSON에 조금 익숙하며 JSON 파일의 데이터에 액세스하는 데 약간의 문제가 있습니다. 지금은 일부 데이터 만 로그 아웃하려고합니다.JSON 데이터에 액세스하기 NativeScript

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" 

      ] 
     } 
    ] 
} 

, 어떤 도움을 주시면 감사 .

답변

2

아마 무슨 일이 일어나고있는 것입니까? 당신은 데이터를 얻고 있지만 그것을 볼 수 있도록 로깅하지 않았을 것입니다. 당신은 일을 :

console.log(response.json()); 

console.dump(response.json()); 

덤프 출력 JSON을하고 시도 로그 문자열을 기록합니다. 따라서 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; 
}); 
+0

이미 JSON 개체 (TypeError : 이미 읽음) 인 응답에서 .json()을 호출하는 오류가 발생한다는 것을 알았습니다. 응답을 많이 주셔서 감사합니다, 나는 확실히 그 console.dump()를 미래에 사용할 것이다. – SunnyBCC

관련 문제