Asp. NET implement to convert JSON data into Ado.net DataSet object _ Practical Tips

Source: Internet
Author: User

There are many ways in which JSON data can be converted to ado.net datasets, and Newtonsoft.json also provides a converter of the dataset to transform JSON data. But in some cases DataSet converter does not work, and it does not necessarily meet the needs of the project. Here is another simple and efficient way to quickly and easily convert JSON data to ado.net datasets.

Design

In fact, Newtonsoft.json has provided a complete set of Json data document structures that provide the object model of 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 The object model of the structure is traversed to achieve ado.net DataTable, DataRelation creation and dataset generation. The basic object model is as follows:

In fact, it is very simple to achieve, I have to complete the project and unit test open source to the GitHub. The specific code can refer to Https://github.com/daxnet/Json2DataSet open source project.

Performance

Performance, in the process of implementation, I found that there are two areas of performance bottlenecks:

1. Regular expressions
2.jtoken.path Property

If you need to call both operations in a high-frequency way in your code, you need to consider performance issues. Especially the Jtoken.path attribute, its internal implementation involves linked list data structure, complex loop, etc., so the efficiency is not very high, should try to avoid using this attribute (of course I open source code in another in the call this attribute, can further optimize).

On the other hand, for the construction of Ado.net DataTable and DataRelation, the performance is still quite high, no need to worry. Overall, it takes only about 4 seconds to convert 15MB of JSON data to a dataset, and interested friends can further optimize that code.

Call
the method of invocation has been explained on the Open Source project home page. Very simple:

Copy Code code as follows:

var json = File.readalltext (@ "D:\test.json");
var dataSet = Json2datasetconverter.convert (JSON);

For example, by invoking a restful API, after obtaining the JSON response, convert the return result directly into a dataset and see customer information for a company in the visual Studio Debug Visualizer for DataSet. and the order data that these customers have completed:

Application

TIBCO Spotfire is a kind of world advanced data analysis software, not only powerful, but also can be customized data import Plug-ins, easy to import the external data into the analysis. It integrates and analyzes data in the form of a table, so its support for the Ado.net dataset is very good and makes it easy to import the table data contained in the Ado.net dataset.

There is an existing restful API that can capture list information for all countries in the world, including country names, languages, population, number of countries bordering the country, and so on. The address of the API is:

Copy Code code as follows:

Get Http://restcountries.eu/rest/v1/all

OK, now I've developed a very simple plugin that can import data from the RESTful API to Tibco Spotfire through 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 the number of countries bordering on China (15).

Related Article

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.