Allan Huston
Allan Huston

Reputation: 17

Converting html to Json object

I'm currently working on a project where I need to convert some older code into a json object. We're taking the result set from a sql query and returning the categories it gives back as json. I'm not that well versed in javascript let alone json so I'm not sure what's the simplest way to go about this. Here is the function I need to change into 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;
}

Here is what I have so far but I am not returning a valid JSON object:

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;
}

If I need to provide anything else please let me know! Thanks!

Upvotes: 1

Views: 8898

Answers (1)

Rei Mavronicolas
Rei Mavronicolas

Reputation: 1435

Do you want to output a javascript object to a string?

Construct the object:

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);

Alternatively, you could construct the object using literals:

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

Then return the object as string.

return JSON.stringify(category);

A reference to Javascript objects if you need more help: http://www.w3schools.com/js/js_objects.asp

Upvotes: 3

Related Questions