Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have changed the static from string to object with no success.

The inputs mo, dy and yr exist.

I have written a working class statement using a string including: idd, datetime, col1, col2 and col3.

How do I get a JSON string out of a class object?

Error Code:
[object Object]

Default.aspx.cs
C#
public static string GetDate(string mo, string dy, string yr)
{
    List<dataclass> returndata = new List<dataclass>();
    dataclass dc = new dataclass();
    var json = "";
    string date1 = mo.ToString() + "/" + dy.ToString() + "/" + yr.ToString() + " 8:00 AM";
    if (date1.Length > 6)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        string connStr = ConfigurationManager.ConnectionStrings["jsonobject"].ConnectionString;
        string cmdStr = "SELECT * FROM [jsondata] WHERE [datetime1]=@datetime1;";
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
            {
                conn.Open();
                cmd.Parameters.AddWithValue("@datetime1", date1);
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        dc.idd = dr["idd"].ToString();
                        dc.datetime = dr["datetime1"].ToString();
                        dc.col1 = dr["col1"].ToString();
                        dc.col2 = dr["col2"].ToString();
                        dc.col3 = dr["col3"].ToString();
                        returndata.Add(dc);
                    }
                }
            }
        }
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        json = serializer.Serialize(returndata);
    }
    else
    {
        json = "mo,dy,yr were null";
    }
    return json;
}
Posted

1 solution

Add Json.NET to your project (use NuGet), then start reading here: http://james.newtonking.com/json/help/index.html[^]
 
Share this answer
 
Comments
Teledextri 17-Dec-14 12:48pm    
What happens if I want to do it only with System.Web.Script.Serialization.JavaScriptSerializer?
Kornfeld Eliyahu Peter 17-Dec-14 15:21pm    
It's OK with me...However consider that it exists only in 4.5 and it's less powerful...
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx
ravithejag 18-Dec-14 6:15am    
Try this http://blogs.microsoft.co.il/pini_dayan/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900