DevExpress、XtraGrid绑定到JSON
Bind to JSON Data | WinForms Controls | DevExpress Documentation
JsonDataSource Class | Cross-Platform Class Library | DevExpress Documentation
要绑定到Code中的JSON格式的数据,请创建一个新的DevExpress.DataAccess.Json.JsonDataSource对象。
private void Form1_Load(object sender, EventArgs e) { gridControl1.DataMember = "Customers"; gridControl1.DataSource = CreateDataSourceFromWeb(); } private JsonDataSource CreateDataSourceFromWeb() { var jsonDataSource = new JsonDataSource(); //Specify the data source location jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json")); jsonDataSource.Fill(); //jsonDataSource.FillAsync(); return jsonDataSource; }
下面的代码示例说明了如何从Web检索JSON数据。
using DevExpress.DataAccess.Json; // ... public static JsonDataSource CreateDataSourceFromWeb() { var jsonDataSource = new JsonDataSource(); // Specify the endpoint.
Uri webUri = new Uri("http://northwind.servicestack.net/customers.json")
jsonDataSource.JsonSource = new UriJsonSource(webUri); // Populate the data source with data. jsonDataSource.Fill(); return jsonDataSource; }
下面的代码示例说明了如何使用文件中的JSON数据。
using DevExpress.DataAccess.Json; // ... public static JsonDataSource CreateDataSourceFromFile() { var jsonDataSource = new JsonDataSource(); // Specify the JSON file name. Uri fileUri = new Uri("customers.json", UriKind.RelativeOrAbsolute); jsonDataSource.JsonSource = new UriJsonSource(fileUri); // Populate the data source with data. jsonDataSource.Fill(); return jsonDataSource; }
下面的代码示例说明了如何使用字符串变量中的JSON数据。
using DevExpress.DataAccess.Json; // ... public static JsonDataSource CreateDataSourceFromText() { var jsonDataSource = new JsonDataSource(); // Specify a string with JSON data. string json = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\",\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\",\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\",\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"}],\"ResponseStatus\":{}}"; // Specify the object that retrieves JSON data. jsonDataSource.JsonSource = new CustomJsonSource(json); // Populate the data source with data. jsonDataSource.Fill(); return jsonDataSource; }
如果您需要排除特定的数据字段,请手动构建JSON Schema。
private JsonDataSource CreateDataSourceFromWeb() { var jsonDataSource = new JsonDataSource(); UriJsonSource uriJsonSource = new UriJsonSource(); jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json")); JsonSchemaNode root = new JsonSchemaNode("root", null); JsonSchemaNode nodeCustomers = new JsonSchemaNode("Customers", true, JsonNodeType.Array); JsonSchemaNode nodeCompanyName = new JsonSchemaNode("CompanyName", true, JsonNodeType.Property, typeof(string)); JsonSchemaNode nodeContactName = new JsonSchemaNode("ContactName", true, JsonNodeType.Property, typeof(string)); JsonSchemaNode nodeContactTitle = new JsonSchemaNode("ContactTitle", true, JsonNodeType.Property, typeof(string)); JsonSchemaNode nodeAddress = new JsonSchemaNode("Address", true, JsonNodeType.Property, typeof(string)); JsonSchemaNode nodeCity = new JsonSchemaNode("City", true, JsonNodeType.Property, typeof(string)); nodeCustomers.Nodes.AddRange(new DevExpress.DataAccess.Node<JsonNode>[] { nodeCompanyName, nodeContactName, nodeContactTitle, nodeAddress, nodeCity, }); root.Nodes.Add(nodeCustomers); jsonDataSource.Schema = root; jsonDataSource.Fill(); //jsonDataSource.FillAsync(); return jsonDataSource; }
posted on 2020-12-10 14:07 springsnow 阅读(733) 评论(0) 编辑 收藏 举报