can someone tells me how to do this?

I have this function in c#:

public string ConvertLocationTableToString()
{
    int radius = 0;
    string locationType = "marker";

    DataTable dt = new DataTable();
    Using (SqlConnection con = new     SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
    {
        Using (SqlCommand cmd = new SqlCommand("SELECT  lat=Latitude, lng=Longitude,    FROM  Locations", con))
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);
        }
    }
}

The table Locations contains two rows with latitude and lontitude values.

It produces string value of:

[{"lat":24.816925048828125,"lng":-107.37641906738281} , {"lat":24.815664291381836,"lng":-107.38169097900391}]

.

But I want to produce the following:

[{"Coordinates": [{"lat":24.816925048828125,"lng":-107.37641906738281}], "Radius": 0,"LocationType": "marker"} ,{ "Coordinates": [{lat":24.815664291381836,"lng":-107.38169097900391}],"Radius": 0,"LocationType": "marker"}}]

Please notice that ‘Radius’ and ‘LocationType’ are not fields in the table.

Thank you for your help.

rubenc

1

There are 1 answers

0
Brian On BEST ANSWER

You are serializing rows, which is returned from the table. If you created an object in C# that matches your desired output, then looped over the rows returned and set the relevant items, you could then serialize that.

In your case, the object would consist of: - Coordinates - some type of list - Radius - int I assume - LocationType - guessing enum

Then you would create an arrray of these, and serialize the array.