2014-01-16 5 views
0

내 노드 응용 프로그램에서 나는 많은 요청을 Object.I로 전달하므로 정확한 형식의 요청이어야합니다.Json 응답 + Node.js

{"q0":{"query":"James Madison","type":"/people/presidents","type_strict":"should"}, 
"q1":{"query":"George Washington","type":"/people/presidents","type_strict":"should"}, 
"q2":{"query":"John Adams","type":"/people/presidents","type_strict":"should"}, 
"q3":{"query":"James Monroe","type":"/people/presidents","type_strict":"should"}, 
"q4":{"query":"Thomas Jefferson","type":"/people/presidents","type_strict":"should"}} 

하지만 내 응답으로오고있다 :

{q0:{query0},q1:{query1},q2:{query1}} 

내 reponse가 (내 응용 프로그램에서) {q0:{response0},q1{response1},q2{response2}

내 실제 쿼리해야합니다 :

내 요청으로 고려

{"result":[q0result,q1result,q3result]} 

내 코드 :

for (var id in presidents) { 
     var match 
     if (query == presidents[id]) { 
     //console.log(" Inside match") 
      match = true; 
     } 
     else { 
      match = false; 
     } 
     matches.push({ 
      "id": id, 
      //"name": name, 
      "score": 100, 
      "match": match, 
      "type": [{ 
       "id": "/people/presidents", 
       "name": "US President" 
      }] 
     }) 
    } 
    callback(matches); 



json = JSON.stringify({"result":matches}); 
    res.writeHead(200, {'content-type':'application/json'}); 
    res.end(json); 

이 문제를 해결하는 데 도움을주십시오. 미리 감사드립니다.

답변

1

에만 한 결과를 반환 ..its 대신 당신이

var matches = {}; 
for (var id in presidents) { 

     if (query == presidents[id]) { 
     //console.log(" Inside match") 
      match = true; 
     } 
     else { 
      match = false; 
     } 
     matches[id] ={ 
      "id": id, 
      //"name": name, 
      "score": 100, 
      "match": match, 
      "type": [{ 
       "id": "/people/presidents", 
       "name": "US President" 
      }] 
     }; 
    } 
    callback(matches); 
+0

감사 @Saravana 쿠마 아래와 같이 결과 객체의 속성을 만들어야합니다 배열에 결과를 추진하고있다 .. – Subburaj

+0

당신을 위해 전 경기를 선언해야 루프, 편집 된 답변보기 –