2012-08-07 104 views
0

我有一个.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。我该如何解决它?

+1

我不明白你的question.if你需要转换器,可以使用http://www.codeplex.com/json/。 – Myd 2012-08-07 08:42:49

+0

我从数据库读取上面的字符串,我想写它到json。例如:{“something”:“+ thisdata +”}。当我这样做时,我有“'期望的十六进制数字'错误”。 – 2012-08-07 08:50:13

+0

你使用的转换器是什么。使用json .net尝试。 http://www.codeplex.com/json/。数据是从db类吗?如果数据是字符串,那么转换必须丢失。 – Myd 2012-08-07 08:52:32

回答

0

JSON需要键值对,在你的情况下它们也应该有字符串引号。

请看下面的例子:

{"content": "\foo\bar"}; 
+0

我不明白你的意思。 – 2012-08-07 09:21:35

+0

我不明白你为什么在我回答了什么之后完全替换了你的问题? – zvona 2012-08-07 10:13:21

+0

@zvona - 老实说,看了几遍,OP的问题非常模糊。无论是原始形式还是当前形式。我认为原始版本是从DB中拉出的富文本的示例,然后他/她更新以向我们显示正在使用的代码。由你决定是否要追求和澄清。 – Kev 2012-08-07 23:45:28