【发布时间】:2015-08-31 20:48:22
【问题描述】:
我有一个角度应用程序,我试图在其中获取特定格式的 json。 我正在从 db 中提取数据,并且数据库的表数据为
Country State
China chungchong
India hyderabad
USA florida
India delhi
USA redmond
我正在尝试获取包含国家/地区列表的 json,并且每个国家/地区都有一个数组中的城市。
angular.module('app')
.factory('CountyStatesService', [
'$resource', function($resource) {
return $resource('/api/GetCountryStates/');
}
])
GetCountryStates() 方法是一个 csharp 方法,它只返回表中所有数据的列表。
我正在尝试按以下格式对 json 进行排序/分组
data: [
{ text: "China", items: [
{text: "chungchong"}
] },
{
text: "India", items: [
{ text: "Hyderabad" },
{ text: "Delhi" }
]
},
{
text: "USA", items: [
{ text: "Florida" },
{ text: "Redmond" }
]
}
]
但是我得到了没有分组的 json。如何按预期实现json格式。
C#代码
[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;
}
}
【问题讨论】:
-
CountrieStatesClass看起来像什么?是否支持你想要的嵌套格式? -
它有字符串 CountryName get;set;和字符串 StateName get;set;
标签: c# json angularjs grouping