There are many ways to convert JSON data to the ADO. Newtonsoft.json also provides a dataset converter to convert JSON data. However, in some cases the dataset converter does not work, and it does not necessarily meet the needs of the project. Here's another simple and efficient way to quickly and easily convert JSON data to an ADO dataset.
Design
In fact, Newtonsoft.json has provided a complete set of Json data document structures, which provides the object model for this document structure under the Newtonsoft.Json.Linq namespace, so we can borrow this model to apply the visitor (visitor,gof95) pattern to the tree To achieve the creation of the ADO, DataRelation, and the dataset by traversing the object model of the The base object model is as follows:
It's actually very simple to implement, and I've already open the whole project and unit tests to GitHub. The specific code can refer to the Https://github.com/daxnet/Json2DataSet Open source project.
Performance
In terms of performance, in the implementation process, I found that there are two performance bottlenecks:
- Regular expressions
- Jtoken.path Property
If you need to call both of these operations at high frequency in your code, you need to consider performance issues. In particular, the Jtoken.path attribute, whose internal implementation involves linked list data structures, complex loops, and so on, so inefficient, should try to avoid the use of this property (of course, my open source code in the call this property, can be further optimized).
On the other hand, for the construction of the ADO and DataRelation, the performance is quite high and there is no need to worry. In general, it takes about 4 seconds to convert 15MB of JSON data into a dataset, and interested friends can further optimize it based on that code.
Call
The method of invocation has been described on the Open Source project home page. Very simple:
var json = File.readalltext (@ "D:\test.json"); var DataSet = Json2datasetconverter.convert (JSON);
For example, by invoking a restful API to get the JSON response, the returned result is converted directly to a dataset, and a company's customer information is seen in the visual Studio Debug Visualizer for DataSet. and the order data that these customers have completed:
Application
TIBCO Spotfire is one of the world's Advanced data analysis software, not only powerful, but also for its custom data import plug-in, easy to import external data into it for analysis. It integrates and analyzes data in the form of tables, so its support for the ADO dataset is very good, and it is easy to import the table data contained in the ADO DataSet.
There is a restful API that can access list information for all countries in the world, including the country name, language, population, number of countries bordering the country, and so on. The address of the API is:
GET Http://restcountries.eu/rest/v1/all
Well, now that I've developed a very simple plugin, you can import data from the RESTful API to Tibco Spotfire with the Get command:
After importing the data, I can immediately analyze which country has the largest population and which country has the highest number of countries bordering it:
See, our country not only has the largest population (19% of the world's population), but also has the largest number of countries bordering China (15).
Convert JSON data to an ADO DataSet object