I have angular application where I am trying to get json in particular format. I am pulling data from db and database has table data as
Country State
China chungchong
India hyderabad
USA florida
India delhi
USA redmond
I am trying to get json with list of countries and each country having cities in an array.
angular.module('app')
.factory('CountyStatesService', [
'$resource', function($resource) {
return $resource('/api/GetCountryStates/');
}
])
GetCountryStates() method is a csharp method which just returns list of all data from the table.
I am trying to get json sorted/grouped by in the below format
data: [
{ text: "China", items: [
{text: "chungchong"}
] },
{
text: "India", items: [
{ text: "Hyderabad" },
{ text: "Delhi" }
]
},
{
text: "USA", items: [
{ text: "Florida" },
{ text: "Redmond" }
]
}
]
But I am getting json with out grouping. How can I achieve json format as expected.
C# code
[HttpGet]
public IEnumerable<CountrieStatesClass> GetCountryState()
{
return GetStructuredJson();
}
public IEnumerable<CountrieStatesClass> GetStructuredJson()
{
List<CountrieStatesClass> locations = new List<CountrieStatesClass>();
using (SqlConnection connection = new SqlConnection(MyConnectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "select * from tblCountryStateDetails";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
CountrieStatesClass location = new CountrieStatesClass
{
CountryName = (string)reader["CountryName"],
StateName=(string)reader["StateName"]
};
locations.Add(location);
}
}
return locations;
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire