2012-02-03 3 views
1

이 문제가 해결되지 않아 json 응답을 반환하는 webService를 호출합니다.JSON.parse 배열에서 데이터를 가져 오는 중

이제 응답에서 특정 값을 가져오고 싶지만 인터넷에서 검색 한 후 많이 고치면 해결할 수 없습니다.

var xhr = Titanium.Network.createHTTPClient({ 

onload : function(e) { 
    Ti.API.info("Received text: " + this.responseText); 
    alert('success'); 
    }, 
// function called when an error occurs, including a timeout 
onerror : function(e) { 
    Ti.API.debug(e.error); 
    alert('error'); 
}, 
timeout : 5000 
}); 

var data = {"data":"system.connect"}; 
xhr.open("POST","http://mytesturl.net/services/json"); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 
xhr.send("method=system.connect"); 

응답은 다음과 같이이다 :

{"#error":false,"#data":{"sessid":"c4likn6vg33hngbpmobisrsbpcjjmf39","user":{"uid":0,"hostname":"102.119.85.120","roles":{"1":"anonymous user"},"session":"","cache":0}},"#response_code":200} 

나는 sessid 값을 가져 오려는 위의 반응에서

여기 내 코드입니다. 올바른 접근 방식은 무엇입니까?

답변

4

다음은 jsfiddle.net에서 확인하고 테스트 할 수 있습니다.

// Given a string of JSON called responseText 
var responseText = '{"#error":false,"#data":{"sessid":"c4likn6vg33hngbpmobisrsbpcjjmf39","user":{"uid":0,"hostname":"102.119.85.120","roles":{"1":"anonymous user"},"session":"","cache":0}},"#response_code":200}'; 

// You can parse it to an object using JSON.parse 
var responseObj = JSON.parse(responseText); 

// And then access the properties as with any object 
console.log(responseObj ["#data"]["sessid"]); 
+0

문제는 'var myData = this.responseText'와 같은 응답을 저장하려고 시도 할 때 문제가 발생하여 Null로 기록됩니다. 내가 왜 null을 보이고 있는지 모르겠지만'this.responseText'처럼 정확한 응답을 준다. –

+0

덕분에 ... 고마워 .... –

관련 문제