如何使用java从文本文件中删除特定的String

时间:2022-05-27 22:23:35

I have a text file which consists of json data and I do not need some of the data fields. How do I remove these strings and bring them to another form?

我有一个由json数据组成的文本文件,我不需要一些数据字段。如何删除这些字符串并将其转换为其他形式?

Current Data:

[{"EMPTYPE":"21","EMPSUM":"1","VERSION":2.2,"UID":212121,"EMPID":454354,"EMPNAME":"abc","EMPGPRID":123,"AID":121,"DATE":"2015-07-07","EMPGPRID":21}]

Required Data:

[{"EMPTYPE":"21","EMPID":454354,"EMPNAME":"abc","EMPGPRID":123}]

Parser's or delimeters

解析器或分层

2 个解决方案

#1


1  

"(?!EMP(?:TYPE|ID|NAME|GPRID)")[^"]*":.*?(?:,|(?=}))

Try this.Replace by empty string.See demo.

尝试使用空字符串替换。查看演示。

https://regex101.com/r/hR7tH4/18

#2


0  

You should look into Jackson for the json parsing (it will take the json string and make more like a regular Java object). It also has a nice remove method you can use (if you know what you don't want) or you can simply make a new object with just the info you like. It's then easy to serialize it back to json.

您应该查看Jackson的json解析(它将采用json字符串并使其更像常规Java对象)。它还有一个很好的删除方法,你可以使用(如果你知道你不想要的东西),或者你可以简单地用你喜欢的信息创建一个新对象。然后很容易将它序列化为json。

#1


1  

"(?!EMP(?:TYPE|ID|NAME|GPRID)")[^"]*":.*?(?:,|(?=}))

Try this.Replace by empty string.See demo.

尝试使用空字符串替换。查看演示。

https://regex101.com/r/hR7tH4/18

#2


0  

You should look into Jackson for the json parsing (it will take the json string and make more like a regular Java object). It also has a nice remove method you can use (if you know what you don't want) or you can simply make a new object with just the info you like. It's then easy to serialize it back to json.

您应该查看Jackson的json解析(它将采用json字符串并使其更像常规Java对象)。它还有一个很好的删除方法,你可以使用(如果你知道你不想要的东西),或者你可以简单地用你喜欢的信息创建一个新对象。然后很容易将它序列化为json。