Reputation: 111
I'm doing an ajax call to retrieve JSON data returned from a query. My JSON is returning the following (accurately):
[{
"label": "",
"value": "2302"
}, {
"label": "9 - Contract set-up in EPICOR",
"value": "2280"
}, {
"label": "2 - Verify PO received",
"value": "2279"
}, {
"label": "7 - Review quote and prepare team for meeting",
"value": "2281"
}]
But I need it to actually return:
{
"options": {
"logan_dvprTasks.taskID": {
[{
"label": "",
"value": "2302"
}, {
"label": "9 - Contract set-up in EPICOR",
"value":"2280"
}, {
"label": "2 - Verify PO received",
"value":"2279"
}, {
"label": "7 - Review quote and prepare team for meeting",
"value":"2281"
}]
}
}
The code I have that generates the JSON is:
public IEnumerable<updatetasks> GetAllItems(string dvprid)
{
string stringSQL = "sqlStatement goes here";
string connString = ConfigurationManager.ConnectionStrings["loganWebConn"]
.ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(connString))
{
sqlConnection.Open();
using (SqlCommand cmd = sqlConnection.CreateCommand())
{
cmd.CommandText = stringSQL;
cmd.CommandType = CommandType.Text;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var updatetasks = new updatetasks();
updatetasks.label = reader["taskName"].ToString();
updatetasks.value = reader["taskID"].ToString();
yield return updatetasks;
}
}
}
}
}
How can I modify my C# code to add in these two items at the start of the JSON object?
Upvotes: 0
Views: 56
Reputation:
By returning the object you want instead.
Change your ajax call to this method and return it as such.
public object MyCall(string dvprid)
{
return new
{
options = new
{
logan_dvprTasks = GetAllItems(dvprid)
};
};
}
Keep in mind that .
(dot) is not valid on a C# identifier, therefore I called your logan_dvprTasks.taskID
simply logan_dvprTasks
.
If you mean taskID
to be a property of logan_dvprTasks
, the resulting JSON would be
{"options":{"logan_dvprTasks":{"taskID":[...]}}}
Upvotes: 1