How to handle this string in json?

Posted by Pandiya Chendur on Stack Overflow See other posts from Stack Overflow or by Pandiya Chendur
Published on 2010-05-31T04:32:53Z Indexed on 2010/05/31 4:42 UTC
Read the original article Hit count: 711

Filed under:
|
|
|

I am storing a multiline textbox value in my db table... When i converted this value to json it gives me an error Error: unterminated string literal...

My sample data was ,

Address Multiline textbox

I am fetching the row to my datatable and then converting it to json,

public string GetJSONString(DataTable table)
    {
        StringBuilder headStrBuilder = new StringBuilder(table.Columns.Count * 5); 
        for (int i = 0; i < table.Columns.Count; i++)
        {
            headStrBuilder.AppendFormat("\"{0}\" : \"{0}{1}¾\",", table.Columns[i].Caption, i);
        }
        headStrBuilder.Remove(headStrBuilder.Length - 1, 1);     
        StringBuilder sb = new StringBuilder(table.Rows.Count * 5); 
        sb.Append("{\"");
        sb.Append(table.TableName);
        sb.Append("\" : [");
        for (int i = 0; i < table.Rows.Count; i++)
        {
            string tempStr = headStrBuilder.ToString();
            sb.Append("{");
            for (int j = 0; j < table.Columns.Count; j++)
            {
                table.Rows[i][j] = table.Rows[i][j].ToString().Replace("'", "");
                tempStr = tempStr.Replace(table.Columns[j] + j.ToString() + "¾", table.Rows[i][j].ToString());
            }
            sb.Append(tempStr + "},");
        }
        sb.Remove(sb.Length - 1, 1); // trim last ,
        sb.Append("]}");
        return sb.ToString();
    }

The above method doen't seem to handle newline character... Any suggestion...

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON