读取JSON文件并 排序,分组,

读取.json文件

public string GetFileJson(string filepath)
{
string json = string.Empty;
using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
{
json = sr.ReadToEnd().ToString();
}
}
return json;
}


返回.json文件

public ActionResult GridPageListJson()
{
try
{
string filepath = Server.MapPath("~/Views/OrderInfoKJ/json1.json");
string json = GetFileJson(filepath);
return Content(json);
}
catch (Exception ex)
{
return null;
}
}

 

 

//--------------------------------------JSON排序 a.properties[field]根据排序的ID
var JsonSort = p.sort(function (a, b) { return a.properties[field] > b.properties[field] ? 1 : -1; });

 


----------------------------------------JSON分组
///json 数组 根据某一字段相同名称进行分组
var arr = eval("(" + da + ")").features;

var map = {},
dest = [];
for (var i = 0; i < arr.length; i++) {
var ai = arr[i];

if (ai.properties[str] == "" || ai.properties[str] == "0" || ai.properties[str] == null || ai.properties[str] == 0 || ai.properties[str] == undefined || ai.properties[str] == false) {
ai.properties[str] = "什么都没有";

}

if (!map[ai.properties[str]]) { //判断没有出现这个字段的数组

dest.push({
id:ai.properties[str],
data: [ai]
});
map[ai.properties[str]] = ai; ///总的分组
} else { ///已经出现过的数组进行循环遍历到已经出现的数组中

for (var j = 0; j < dest.length; j++) {
var dj = dest[j];
///把相同的元素添加到data里面去
if (dj.id == ai.properties[str]) {
dj.data.push(ai);
break;
}
}
}

} console.log(dest)

posted @ 2017-05-19 13:25  陌陌逸帆  阅读(780)  评论(0编辑  收藏  举报