2013-07-03 4 views
0

저는 현재 일부 구형 코드를 json 객체로 변환해야하는 프로젝트에서 작업하고 있습니다. 결과를 SQL 쿼리에서 가져 와서 json으로 반환하는 범주를 반환합니다. 나는 자바 스크립트에 정통한 사람이 아니기 때문에 json 만 알면, 이것에 대해 가장 간단한 방법은 무엇인지 모르겠습니다. 여기html을 Json 객체로 변환하기

function createOutputCategories(){ 
try 
    { 
     output = 
       "<html>" + 
       "<head>" + 
       "<title>" + 
       "You can find it!" + 
       "</title>" + 
       "</head>" + 
       "<body bgcolor='#CED3F3'>" + 
       "<a href='" + url + "file.xsjs?parent=1'>" + 
       "</a>" + 
       "<br><br>"; 
     if(parent === "1"){ 
      output = output + "<h3><font color='#AAAAAA'>Home</font>"; 
     }else{ 
      output = output +"<a href='javascript:history.back()'>" + 
      "<h3>Back"; 
     } 
     output = output + 
       "</h3>" + 
       "</a>" + 
       "<h1>" + 
       "Categories:" + 
       "</h1>"; 

     while(rs.next()){ 
      if(rs.getString(3) === 0 || rs.getString(3) === null || rs.getString(3) === undefined || rs.getString(3) === "0"){ 
       output = output + "<br><a href='" + url + "yeti.xsjs?parent=" + rs.getString(1) + "'>" + rs.getString(2) + "</a>"; 
      }else{ 
       output = output + "<br><a href='" + url + "yeti.xsjs?parent=" + rs.getString(1) + "'>" + rs.getString(3) + "</a>"; 
      } 
     } 
}catch(Exception){ 
    $.response.contentType = "text/plain"; 
    $.response.setBody("Failed to retreive data"); 
    $.response.status = $.net.http.INTERNAL_SERVER_ERROR; 
} 

내가 지금까지 가지고 있지만 유효한 JSON 개체를 반환하고 있지 않다 무엇인가 : 내가 무엇을하시기 바랍니다 제공해야하는 경우

function createOutputCategories(){ 

try{ 
    output = 
     "category: {name = \"" + parent + "\"; description = \"\"}"; 

    output = output + 
     "subcategories: [ "; 

    while(rs.next()){ 
     output = output + 
      "{ catid = \"" + rs.getString(1) + "\"; catname = \"" + rs.getString(2) + "\"; altname = \"" + rs.getString(3) + "\"; description = \"" + rs.getString(4) + "\"}"; 
     } 
    output = output + 
     "];"; 
    } 
catch(Exception){ 
    $.response.contentType = "text/plain"; 
    $.response.setBody("Failed to retreive data"); 
    $.response.status = $.net.http.INTERNAL_SERVER_ERROR; 
} 

을 여기에 내가 JSON으로 변경해야하는 기능입니다 알려줘! 감사!

+0

당신은 고전적인 자바 스크립트 객체를 구축 Should'nt을 다음 JSON.stringify을 사용할 수 있습니까? –

+0

이것을 확인해보십시오 : http://json.fastfrag.org/ –

+0

@SteveB 저는 제가 지금까지 알고있는 것을 적용해서이 작업을하고 싶습니다. 객체를 만드는 것이 이상적 일지 모르지만 나는 할 수 없었습니다. 이걸 제대로 수행하십시오 –

답변

3

자바 스크립트 개체를 문자열로 출력 하시겠습니까? 당신은 리터럴을 사용하여 객체를 생성 할 수, 또는

var category=new Object(); 
category.name="Name"; 
category.description="My lovely description"; 
category.subcategories=[]; 

var subCat=new Object(); 
subCat.catid=1; 
subCat.catname="My subcat"; 

category.subcategories.push(subCat); 

:

개체를 구축

var category={ 
    name:"Name", 
    description:"My lovely description", 
    subcategories:[ 
     {catid:1,catname:"My subcat"} 
    ] 
}; 

그런 다음 문자열로 오브젝트를 돌려줍니다.

return JSON.stringify(category); 

자바 스크립트 객체에 대한 참조입니다 도움이 더 필요한 경우 : http://www.w3schools.com/js/js_objects.asp

+0

개체를 만든 다음 나중에 목적으로 문자열을 반환하는 것이 가장 좋습니다. 내가 배우러 가려고/이해가/내가 하하 가서 모든이 적용 –

+0

내가 자바 스크립트 예제를 조금 객체를 만드는 다른 방법을 보여주기 위해 확장했습니다. –

+0

나는 이것을 시험해 볼 것이다, 고마워! –

관련 문제