我目前正在开发一个项目,我需要将一些旧代码转换为json对象。我们从sql查询中获取结果集,并将其返回的类别作为json返回。我不太熟悉javascript,更不用说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;
}
让我知道!谢谢!
Should'nt你建立一个经典的JavaScript对象,然后使用JSON.stringify? –
检查了这一点:http://json.fastfrag.org/ –
@SteveB我想我只是应用到目前为止我所知道的,并使这项工作,创建一个对象将是理想的,但我一直无法正确地做到这一点 –