In ASP. NET, Json data is converted to ADO. NET DataSet object, jsonado.net
There are many methods to convert Json data to ADO. NET DataSet. Newtonsoft. Json also provides DataSet Converter to convert Json data. However, in some cases, DataSet Converter does not work and may not meet the project's needs. This article introduces another simple and effective method to convert Json data into ADO. NET DataSet.
Design
Actually Newtonsoft. json has provided a complete Json data document structure, Newtonsoft. json. the object model of this document structure is provided in the Linq namespace, so we can use this model to apply the Visitor (Visitor, GoF95) mode on it, traverse the object model of the tree structure to achieve ADO. NET able, DataRelation creation, and DataSet generation. The basic object model is as follows:
In fact, the implementation is very simple. I have open-source the complete project and unit test to GitHub. For specific code, refer to https://github.com/daxnet/json2datasetopen source project.
Performance
In terms of performance, during implementation, I found two bottlenecks:
1. Regular Expression
2. JToken. Path attribute
If you need to call these two operations frequently in your code, you need to consider performance issues. Especially JToken. path attribute. Its internal implementation involves the Linked List data structure, complex loops, and so on, so the efficiency is not very high, avoid using this attribute whenever possible (of course, I still have a call to this attribute in the open-source code, which can be further optimized ).
On the other hand, for the construction of ADO. NET DataTable and DataRelation, the performance is still quite high, don't worry. In general, it takes about 4 seconds to convert 15 MB of Json data to DataSet. If you are interested, you can further optimize the code.
Call
The call method is described on the open-source project homepage. Simple:
Copy codeThe Code is as follows:
Var json = File. ReadAllText (@ "d: \ test. json ");
Var dataSet = Json2DataSetConverter. Convert (json );
For example, after a RESTful API is called to obtain the Json Response, the returned result is directly converted to DataSet, and the customer information of a company is displayed in Visual Studio Debug Visualizer for DataSet, and the order data completed by these customers:
Application
TIBCO Spotfire is a world-advanced data analysis software. It is not only powerful, but also can customize data import plug-ins for it to import external data for analysis. It integrates and analyzes data in the form of tables. NET DataSet support is very good, can be very convenient to the ADO.. NET DataSet.
A RESTful API can be used to obtain information about all countries in the world, including country names, languages, population, and the number of countries that border the country. The API address is:
Copy codeThe Code is as follows:
GET http://restcountries.eu/rest/v1/all
Now I have developed a very simple plug-in. You can use the GET command to import data from the RESTful API to the TIBCO Spotfire:
After the data is imported, I can immediately find out which country has the largest population proportion and which country has the most border with it:
We can see that our country has the largest population (19% of the world's population) and the largest number of countries that border China (15 ).