Piraba
Piraba

Reputation: 7014

JSON remove special characters

I want to do the replication between Android sqlite & MS SQL server.That Time i want to take Tables values from Databse.

This is my 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
      }
   ]
}

In Here JSON Description column value contain "(double quotation) .If we check http://jsonformatter.curiousconcept.com/ , it show error.Its a Invalid JSON.

WCF service I have converted DataSet to JSON. Some table column contain special charters.

I converted like this :

       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";
        }
    }

My problem is removing special charters OR How to allow special characters.?

Please help me anybody...

Upvotes: 0

Views: 1644

Answers (2)

Arun Rana
Arun Rana

Reputation: 8606

try to use inbuilt json serialization

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;

    }

Upvotes: 1

Mark
Mark

Reputation: 1748

You shouldn't use a StringBuilder to convert an object to a JSON string. Use the JsonConverter class in JayRock JSON library and it takes care of Serialising/Deserialising Json for you (including escaping)

Upvotes: 2

Related Questions