【问题标题】:JSON remove special charactersJSON删除特殊字符
【发布时间】:2011-12-05 11:19:28
【问题描述】:

我想在 Android sqlite 和 MS SQL server 之间进行复制。那个时候我想从 Databse 中获取 Tables 值。

这是我的 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/ ,它会显示错误。它是一个无效的 JSON。

WCF 服务 我已将 DataSet 转换为 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";
        }
    }

我的问题是删除特殊章程或如何允许特殊字符。?

请任何人帮助我...

【问题讨论】:

    标签: android wcf json


    【解决方案1】:

    您不应使用 StringBuilder 将对象转换为 JSON 字符串。使用 JayRock JSON 库中的 JsonConverter 类,它会为您处理序列化/反序列化 Json(包括转义)

    【讨论】:

      【解决方案2】:

      尝试使用内置的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;
      
          }
      

      【讨论】:

        猜你喜欢
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-14
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多