Existing a LIST:
[
{
"type":"呼吸系统",
"illness":"肺气肿",
"quotaName": "血压"
},
{
"type":"呼吸系统",
"illness":"肺气肿",
"quotaName": "血常规"
},
{
"type":"呼吸系统",
"illness":"哮喘",
"quotaName": "血常规"
},
{
"type":"循环系统",
"illness":"高血压",
"quotaName": "心电图"
},
{
"type":"循环系统",
"illness":"高血压",
"quotaName": "心电彩超"
}
]
The list I want:
[
{
"type":"呼吸系统",
"illnessList":[
{
"name":"肺气肿",
"quotaList":[
{
"name":"血压"
},
{
"name":"血常规"
}
]
},
{
"name":"哮喘",
"quotaList":[
{
"name":"血常规"
}
]
}
]
},
{
"type":"循环系统",
"illnessList":[
{
"name":"高血压",
"quotaList":[
{
"name":"心电图"
},
{
"name":"心电彩超"
}
]
}
]
}
]
The original list, all disease systems, diseases, and disease detection indicators are combined
I want to get a list based on type classification, but I still can’t find the idea
Loop, after traversing it many times, I feel my head is confused, please give me some ideas
According to the input and output requirements of the subject, convert from one
JSONArray
to anotherJSONArray
... It feels like the output JSONArray is just classified according to two attributes. In fact, the structure should be similar toMap<String, Map<String,List<String>>
, so my idea is to convert the inputJSONArray
into the structure ofMap<String, Map<String,List<String>>
...and Seeing what I just said about classifying according to attributes... then... obviously... thump thump! ! ! ...Java8'sCollectors.groupingBy
naturally came to mind... Just do it without thinkinggroupingBy
The following is my little idea and code:
Since it is object-oriented, I first created an input Bo object
FromDataBo
Then there is the output object created according to the output format
ToDataBo
(You can ignore the annotated method first...it is just for conversion, you can look at the data structure first)Now that the input and output objects are in place, the most important classification can be done by attributes. I will first post the code that converts it into
Map<String, Map<String, List<String>>
... That’s the main thing. ..Comment, if you are familiar withlamdba
, you can probably tell it at a glance... If you are not familiar, just learn moreThe last is the complete test code and results. The final
result
object is the output you needJSONArray
Test results:
Just so...
Isn’t this spit out from the backend? There is no need for you to handle it, just tell the backend. Or you can just use what they spit out to achieve your effect.
It’s just that you want to save trouble. Process directly in a loop.
Use the above array and do the processing in the loop, which is the same as what you process later. Not as good as pre-processing. High performance. The following is the standard style.
The interface is posted.
Code address
http://jsbin.com/roqejoficu/e...
The writing is not good. If there is a better method, I hope to share it