2011-12-05 228 views
0

我想做的SQL SQLite & MS SQL服務器之間的複製。那時我想從Databse表中取值。JSON刪除特殊字符

這是我的JSON

 { 
    "Table1":[ 
     { 
     "BusinessUnit":"MASS", 
     "ProductCode":"SLD0201", 
     "Description":"Lou Difan C.Blue 12"3- Commode", 
     "Description2":"301 0201" 

     }, 


     { 
     "BusinessUnit":"MASS", 
     "ProductCode":"SLN0502", 
     "Description":"Lou Napoli I"vory- Cistern", 
     "Description2":"2011 0502" 

     }, 

     { 
     "BusinessUnit":"MASS", 
     "ProductCode":"LDMBL6H", 
     "Description":"Dortek Taper Bullet Handle 6"5 serr ", 
     "Description2":"Taper Bullet Ha" 

     } 

    ], 
    "Table2":[ 
     { 
     "chk":6, 
     "currentchk":1 
     } 
    ] 
} 

在這裏JSON描述列值包含「(雙引號)。如果我們檢查http://jsonformatter.curiousconcept.com/,它表明error.Its一個無效的JSON。

WCF服務,我有。轉換後的數據集,以JSON有些表列包含特殊包機

我轉換是這樣的:

 public String ConverTableToJson(DataSet dsDownloadJson,int currentSplit) 
    { 
     StringBuilder Sb = new StringBuilder(); 
     String result = ""; 
     int start = 0; 
     int end =500; 
     int chk = 0; 
     int currentChk = currentSplit; 
     if (dsDownloadJson.Tables.Count > 0) 
     { 
      Sb.Append("{"); 
      foreach (DataTable dt in dsDownloadJson.Tables) 
      { 
       DataTable dtDownloadJson = dt; 
       string[] StrDc = new string[dtDownloadJson.Columns.Count]; 
       string HeadStr = string.Empty; 
       double total = dtDownloadJson.Rows.Count; 
       Console.WriteLine("--1--" + dtDownloadJson.Rows.Count); 
       if (dtDownloadJson.Rows.Count < 500) 
       { 
        end = dtDownloadJson.Rows.Count; 
       } 

       if (chk == 0) 
       { 
        if (dtDownloadJson.Rows.Count > 500) 
        { 
         if ((dtDownloadJson.Rows.Count/500) == 0) 
         { 
          chk = dtDownloadJson.Rows.Count/500; 
         } 
         else 
         { 
          chk = dtDownloadJson.Rows.Count/500 + 1; 
         } 
        } 
        else 
        { 
         chk = 1; 
        } 
        currentChk = 1; 
       } 
       else 
       { 
        currentChk = currentChk + 1; 
        start = currentChk * 500; 
        end = start + 500; 
        currentChk = chk; 
       } 
       Sb.Append("\"" + dtDownloadJson.TableName + "1\" : ["); 

       if (dtDownloadJson.Rows.Count > 0) 
       { 
        for (int i = 0; i < dtDownloadJson.Columns.Count; i++) 
        { 
         StrDc[i] = dtDownloadJson.Columns[i].Caption; 
         HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\","; 
        } 

        if (HeadStr.Length > 0) 
        { 
         HeadStr = HeadStr.Substring(0, HeadStr.Length - 1); 
         Console.WriteLine("--2--" + start); 
         Console.WriteLine("--3--" + end); 
         for (int i = start; i < end; i++) 
         { 

          string TempStr = HeadStr; 
          Sb.Append("{"); 
          for (int j = 0; j < dtDownloadJson.Columns.Count; j++) 
          { 
           TempStr = TempStr.Replace(dtDownloadJson.Columns[j] + j.ToString() + "¾", dtDownloadJson.Rows[i][j].ToString()); 
           TempStr = TempStr.Replace(""", '\"'); 
          } 

          Sb.Append(TempStr + "},"); 
         } 

         Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1)); 
        } 

       } 
       else 
       { 

       } 
       Sb.Append("],"); 
       if (chk > 1) 
       { 
        Sb.Append("\"Table2\": [{\"chk\": " + chk + ", \"currentchk\": " + currentChk + " }]"); 
       } 
       else 
       { 
        Sb.Append("\"Table2\": [{\"chk\": " + chk + ", \"currentchk\": " + currentChk + " }]"); 
       } 


      } 
      // Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1)); 
      Sb.Append("}"); 
      return Sb.ToString(); 
     } 
     else 
     { 
      return "0"; 
     } 
    } 

我的問題是刪除特殊章程或如何允許特殊字符。

請幫我...人

回答

2

你不應該使用StringBuilder來將對象轉換爲一個JSON字符串。使用JsonConverter類JayRock JSON庫,它負責連載/ Deserialising的JSON對您(包括轉義)

1

嘗試使用內置JSON序列

public static string Serialize<T>(T obj) 

    { 
     System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new 

     System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType()); 
     MemoryStream ms = new MemoryStream(); 
     serializer.WriteObject(ms, obj); 
     string retVal = Encoding.Default.GetString(ms.ToArray()); 
     ms.Dispose(); 
     return retVal; 

    }