Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Background:

In the process of data processing, the standard JSON is as we know, but because each have different needs, we need to sort the data for data cleaning and filtering (that is, we often say the data conversion), but these will encounter a lot of problems in the transformation, such as the problems in the collection of data, how the two sets of data filtering, how to get the data we need, How to write good conditions to manage

Key points:

I choose the way I deal with data format in my normal development process.

Scenario 1:

We want to make an interface. The requirement is to synchronize data from other systems to our system. Maybe there is a field (same time period or people with the same mobile phone number) that is the same.

Analysis: For example, the synchronization of commodity information, (Taobao already has, AS a shopkeeper, I want to go to Jingdong shop, and then do not want to upload a copy of the data, I directly provide a copy of my Taobao commodity information, see how this interface support)

In fact, this requirement is essentially: filter the information obtained into standard input information, and then write the library operation after judging the validity of the data;

  • Filter the information obtained by the criteria
  • Verify data validity
  • Writing library

When we get the data, it’s usually in JSON format,

It’s usually an object in which one of the properties is a set, for example

{" id ": 1626164850455," appID ":" 084597 ", "body" : {" total ": 2," goodInfo ": [{" devName" : "test 112", "devSn" : "ceshi_0013434", "productCode": "834745951011323904", "factoryName": "ceshi", "devMajor": "ceshi", "regionId": 320000, "isInnerNet": 0, "devAddr": "121.12.131.210", "outerId": "ceshinihao"}, {"devName": "Test 122", "devSn": "ceshi_023434", "productCode": "834745951011323904", "factoryName": "ceshi2", "devMajor": "ceshi1", "regionId": "IsInnerNet ": 0, "devAddr": "121.12.131.210", "outerId": "ceshinihao"}]}Copy the code

For example, in the JSON format above, if it is a return format, we directly go to the data in the data;

Get the data and parse the data:

Ideas:

For this kind of JSON, we can parse the googInfo property clear, we can set up the class, and then according to the fastJSON parse object, collate, see the following code:

// The result is the returned JSON data, which is a String; /** 1. Convert result to object format 2. */ JSONObject resultInfo= json.parseObject (result.getData ()) /** GoodInfo GoodInfo =resultInfo.get(" GoodInfo ", goodinfo.class) if there is a collection, then you need to receive the collection with jsonArray */ //GoodInfo GoodInfo = json.parseObject (result.getData (), GOOdInfo. Class) GOOdInfo GOOdInfo =resultInfo.get(" GOOdInfo ", goodinfo.class) // JSONArray goodInfoList= Resultinfo.getjsonarray ("goodInfo") goodinfolist.stream ().map (a->{// Use dozer to convert data from each pipe to a standard, Object Test Test = dozerBeanMapper. The map (a, the Test. Class); return test; }) we can also add a filter criterion to the stream for multiple sets of data. FilterCopy the code

Stream () transforms each element in the pipe using the map method of stream () :

// Parse the data stream into a single object through the pipeline service map, which is transformed by dozerMap. Collect (Collectors. ToList) List<Test> collect= goodinfo.stream ().map(goodInfo->{Test test=dozerBeanMapper.map(a,Test.class); return test; }).filter(goodInfo->stringUtils.isNotEmpty(goodInfo.getName())).collect(Collectors.toList)Copy the code

Directly parsed into, acquired information – parsed into object collection;

In the above code, dozer is an open source plug-in for object median values, which are mainly assigned to each other

I put the code format POM file address below:

<dependency> <groupId>com.github.dozermapper</groupId> <artifactId>dozer-spring-boot-starter</artifactId> </dependency> @autoWired private DozerMppaer dezerMapper;Copy the code

Welcome everyone to correct mistakes, or there is a better way, can also recommend with me