Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with data in json easily and quickly by java

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people have no idea about how to deal with the data in json simply and quickly. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The runners field of JSONstr.json is a subdocument, and the subdocument has three fields: horseId, ownerColours, and trainer, where trainer contains the subordinate field trainerId. You need to query the horseId, ownerColours, and trainerId fields of the corresponding subdocuments according to the document serial number. Some of the source data are as follows:

[

{

"race": {

"raceId": "1.33.1141109.2"

"meetingId": "1.33.1141109"

}

...

"numberOfRunners": 2

"runners": [

{

"horseId": "1.00387464"

"trainer": {

"trainerId": "1.00034060"

}

"ownerColours": "Maroon,pink sleeves,dark blue cap."

}

{

"horseId": "1.00373620"

"trainer": {

"trainerId": "1.00010997"

}

"ownerColours": "Black,emerald green cross of lorraine,striped sleeves."

}

]

}

...

]

Expect the results of the structured Json:

Java only provides the basic class library for parsing Json, but API interfaces are numerous and tedious. If you want to carry out in-depth calculation, you still need complex hard coding. It should be written like this:

...

JSONObject jsonObject = JSONObject.fromObject (s)

JSONArray result = jsonObject.getJSONArray ("runners")

For (int I = 0; I < result.size (); iTunes +) {

JSONArray index = result.getJSONObject (I) .getJSONArray ("index")

...

}

...

Json parsing, using the aggregator SPL will be much easier, it does the secondary encapsulation of the Json class library, and has a rich and powerful set operation ability, so it is easy to deal with. For example, for the above question, take out the first runners field (subdocument) and only need 3 lines:

A

one

= json (file ("/ workspace/JSONstr.json") .read ()

two

= A1 (1) .runners

three

= A2.new (horseId,trainer.trainerId:trainerId,ownerColours)

Json is generally parsed for calculation, so SPL can also be calculated more easily, for example, counting the number of ownerColours occurrences in each group by horseId grouping; just add one line to this: = A3.groups (horseId;~.array (). Count (): times).

After reading the above, have you mastered how java can easily and quickly process the data in 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report