In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Java embedded script how to quickly parse multi-layer json, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
There are many open source packages about Json processing, such as json-lib, jackson, Gson, Fastjson. Gson is made by Google and powerful; Fastjson is made by Alibaba with fast performance. Which one to use, as long as you are happy.
Json parsing is generally for re-calculation, most of these open source packages provide rich interfaces for reading and parsing Json, but then how to filter, deeply calculate and expand multi-tier Json data into two-dimensional data for use by other services still requires complex hard coding, and generally requires the help of relational databases (creating temporary tables, parsing and storing them Then use SQL to calculate it twice, etc.), these steps will require developers to have a headache.
For example, to deal with such a scenario: the Json data of order information is divided into two layers: the first layer is the country and region, and the second layer is the detailed data. Now I want to find out the orders for North China in 2013.
Some of the source data are as follows:
[{"COUNTRY": "China", "AREA": "Northeast China", "ORDERS": [
{"ORDER_ID": 10252, "CUSTOMER_ID": "SUPRD", "EMPLOYEE_ID": 4,... }
{"ORDER_ID": 10318, "CUSTOMER_ID": "ISLAT", "EMPLOYEE_ID": 8,... }
[] }
{"COUNTRY": "China", "AREA": "East China", "ORDERS": [
{"ORDER_ID": 10249, "CUSTOMER_ID": "TOMSP", "EMPLOYEE_ID": 6,... }
{"ORDER_ID": 10251, "CUSTOMER_ID": "VICTE", "EMPLOYEE_ID": 3,... }
[] }
[]
Expect Json to be structured and filtered:
COUNTRY
AREA
ORDER_ID
CUSTOMER_ID
EMPLOYEE_ID
ORDER_DATE
China
North China
10402
ERNSH
eight
2013-01-02
China
North China
10403
ERNSH
four
2013-01-03
China
North China
10404
MAGAA
two
2013-01-03
China
North China
10407
OTTIK
two
2013-01-07
Part of the code for Java parsing should be written like this:
...
JSONObject jsonObject = JSONObject.fromObject (orderstr)
JSONArray jsonArray = jsonObject.getJSONArray ("COUNTRY")
...
JSONArray twos = jsonArray.getJSONArray ("ORDERS")
JSONObject two = null
List list = new ArrayList ()
For (int I = 0; I < twos.size (); iTunes +) {
Two = twos.getJSONObject (I)
Map map = new HashMap ()
...
List.add (map)
}
...
If there is an aggregator, Json parsing will be much easier. It not only encapsulates the Json class library twice and needs less code to implement the same algorithm, but also carefully designs a set of function libraries in the field of set operations to deal with all kinds of structured operations, and it is no longer necessary to install and configure third-party database software. For example, from parsing to filtering, it only takes 4 lines:
A
one
= json (file ("orders.json") .read ()
two
= A1.select (COUNTRY== "China" & & AREA.contain ("North China"))
three
= A2.news (ORDERS;COUNTRY,AREA,$ {B1.ORDERS.fname () .concat@c ()})
four
= A3.select (year (ORDER_DATE) = = 2013)
After reading the above, have you mastered how the Java embedded script can quickly parse multi-tier json? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.