1

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!

4
  • Should'nt you build an classic javascript object , then use JSON.stringify ? Commented Jul 3, 2013 at 9:59
  • check this out: json.fastfrag.org Commented Jul 3, 2013 at 10:02
  • @SteveB I would like to I was just applying what I know so far and make this work, creating an object would be ideal but I haven't been able to do this correctly as of yet Commented Jul 3, 2013 at 10:08
  • Look at Reinard Mavronicolas' answer. This is exactly what I meant. Manual string construction may lead to nightmare when you will have to deal with correct string escaping, versus the native transformation. Commented Jul 3, 2013 at 11:01

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I think it would be best to create an object then return it as a string later for my purposes. I'm trying to learn/understand/apply all this as I go haha
I expanded my javascript example a bit to show you different ways of creating an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.