格式化 json 数据以按项目分组
Formatting json data to group by items
我有 angular 应用程序,我正在尝试以特定格式获取 json。
我正在从数据库中提取数据,数据库中有 table 数据为
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 方法,它只是 returns 来自 table.
的所有数据列表
我正在尝试通过以下格式获取 json sorted/grouped
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
看起来更像这样
public class CountrieStatesClass
{
public string Text {get; set;}
public List<State> Items {get; set;}
}
public class State
{
public string Text {get; set;}
}
然后,像这样加载你的数据
CountrieStatesClass location = new CountrieStatesClass
{
Text = (string)reader["CountryName"],
Items = new List<State>{new State{ Text = (string)reader["StateName"] } }
};
locations.Add(location);
然后,你用linq
return locations
.GroupBy(l => l.Text)
.Select(grp => new CountrieStatesClass
{
Text = grp.Key,
Items = grp.SelectMany(l => l.Items).ToList()
})
.ToList();
然后您只需使用您已经在使用的任何序列化工具对该对象进行序列化。
注意 有更好的方法来加载数据。我个人使用 Entity Framework,但也有许多其他工具。
您可以在一个 Linq 语句中完成所有操作。
public IHttpActionResult Get(Guid id)
{
...
var answers = objList.GroupBy(x => x.Category).Select(x => new { Category = x.Key, Items = x.ToArray() });
response.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(answers), System.Text.Encoding.UTF8, "application/json");
response.Headers.Add("total", objList.Count().ToString());
return ResponseMessage(response);
}
我有 angular 应用程序,我正在尝试以特定格式获取 json。 我正在从数据库中提取数据,数据库中有 table 数据为
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 方法,它只是 returns 来自 table.
的所有数据列表我正在尝试通过以下格式获取 json sorted/grouped
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
看起来更像这样
public class CountrieStatesClass
{
public string Text {get; set;}
public List<State> Items {get; set;}
}
public class State
{
public string Text {get; set;}
}
然后,像这样加载你的数据
CountrieStatesClass location = new CountrieStatesClass
{
Text = (string)reader["CountryName"],
Items = new List<State>{new State{ Text = (string)reader["StateName"] } }
};
locations.Add(location);
然后,你用linq
return locations
.GroupBy(l => l.Text)
.Select(grp => new CountrieStatesClass
{
Text = grp.Key,
Items = grp.SelectMany(l => l.Items).ToList()
})
.ToList();
然后您只需使用您已经在使用的任何序列化工具对该对象进行序列化。
注意 有更好的方法来加载数据。我个人使用 Entity Framework,但也有许多其他工具。
您可以在一个 Linq 语句中完成所有操作。
public IHttpActionResult Get(Guid id)
{
...
var answers = objList.GroupBy(x => x.Category).Select(x => new { Category = x.Key, Items = x.ToArray() });
response.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(answers), System.Text.Encoding.UTF8, "application/json");
response.Headers.Add("total", objList.Count().ToString());
return ResponseMessage(response);
}