Read JSON files and sort, group,

Source: Internet
Author: User

Read the. json file

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;
}


return. json file

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 sort A.properties[field] based on the sorted ID
var jsonsort = P.sort (function (A, b) {return A.properties[field] > B.properties[field]? 1:-1;});


----------------------------------------JSON grouping
JSON arrays are grouped according to the same name of a field
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] = "nothing";

}

if (!map[ai.properties[str]]) {//To determine that no array of this field appears

Dest.push ({
ID:AI.PROPERTIES[STR],
Data: [AI]
});
MAP[AI.PROPERTIES[STR]] = AI; Total grouping
} else {////An array that has already occurred loops through the array that has occurred

for (var j = 0; J < Dest.length; J + +) {
var DJ = Dest[j];
Add the same elements to the data.
if (dj.id = = Ai.properties[str]) {
Dj.data.push (AI);
Break
}
}
}

} console.log (dest)

Read JSON files and sort, group,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.