我有一个.ashx
文件,它从web服务读取一些内容,并且webservice返回一个JSON字符串。富文本json字符串
在WebService的方法是这样的:
SqlConnection connection1 = new SqlConnection("Data Source=.;Initial Catalog=DB;Persist Security Info=True;User ID=User;Password=passwd");
connection1.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Table where sth=" + txt, connection1);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0) return "0";
string json = "{ \"person\": {";
json += " \"info\": [";
json += "{ \"name\": \"" + dt.Rows[0]["name"].ToString() + "\", ";
json += " \"surname\": \"" + dt.Rows[0]["surname"].ToString() + "\", ";
json += " \"id\": \"" + dt.Rows[0]["id"].ToString() + "\", ";
json += " \"birthdate\": \"" + dt.Rows[0]["brthdate"].ToString() + "\", ";
json += " \"result\": [";
for (int i = 0; i < dt.Rows.Count; i++)
{
json += "{\"name\":\"" + dt.Rows[i]["name"].ToString() + "\", ";
/* this is the field returns rich text */
json += "\"value\":\"" + dt.Rows[i]["value"].ToString() + "\"}, ";
/* */
}
json = json.Substring(0, json.Length - 1);
return json + "]}}";
我使用JSON在客户端:
$(document).ready(function() {
$.ajax({
/* on generic handler the webservice which contains the method is calling */
url: "../GenericHandler.ashx?txt='" + QueryString('ID') + "'",
success: function (data) {
write(data);
},
error: function (data) { }
});
});
function write(data) {
var obj = eval("(" + data + ")");
document.getElementById('<%=Label1.ClientID %>').innerHTML = (obj.person.info[0].name);
document.getElementById('<%=Label2.ClientID %>').innerHTML = (obj.person.info[0].surname);
document.getElementById('<%=Label3.ClientID %>').innerHTML = (obj.person.info[0].id);
document.getElementById('<%=Label4.ClientID %>').innerHTML = (obj.person.info[0].birthdate);
var result = "";
result = "<table border=\"1\" cellpadding=\"3\">";
result += "<tr><th>Name</th><th>Value</th></tr>";
for (var i = 0; i < obj.person.result.length; i++) {
result += "<tr><td>" + obj.person.result[i].name + "</td>";
result += "<td>" + obj.person.result[i].value + "</td></tr>";
}
result += "</table>";
$('#Result').html(result);
}
这里是一个样本富文本:
{\rtf1\ansi\ansicpg1254\uc1\deff0{\fonttbl
{\f0\fswiss\fcharset162\fprq2 Arial;}
{\f1\fswiss\fcharset162\fprq2 Microsoft Sans Serif;}
{\f2\froman\fcharset2\fprq2 Symbol;}}
{\colortbl;\red0\green0\blue0;\red255\green255\blue255;}
{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}
{\*\generator TX_RTF32 14.0.520.500;}
\deftab1134\paperw12240\paperh15840\margl1440\margt1440\margr1440\margb1440\widowctrl\formshade\pard\itap0\nowidctlpar\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\tx9360\tx10080\plain\f1\fs17
\sectd\headery720\footery720\pgwsxn12240\pghsxn15840\marglsxn1800\margtsxn1440\margrsxn1800\margbsxn1440\par }
当我运行的代码我有'Expected hexadecimal digit' error
在
var obj = eval("(" + data + ")");
line。我该如何解决它?
我不明白你的question.if你需要转换器,可以使用http://www.codeplex.com/json/。 – Myd 2012-08-07 08:42:49
我从数据库读取上面的字符串,我想写它到json。例如:{“something”:“+ thisdata +”}。当我这样做时,我有“'期望的十六进制数字'错误”。 – 2012-08-07 08:50:13
你使用的转换器是什么。使用json .net尝试。 http://www.codeplex.com/json/。数据是从db类吗?如果数据是字符串,那么转换必须丢失。 – Myd 2012-08-07 08:52:32