gpt4 book ai didi

java - java中的json预处理

转载 作者:行者123 更新时间:2023-11-30 07:18:09 25 4
gpt4 key购买 nike

我有带有不同字段名的 json,其中包含我感兴趣的值。它们来自不同的来源。我想把我感兴趣的那些领域变成一个通用的名字。例如:

这是原始的 json:

{"movieName":"A","Leadactor":"","leadActress":"","movieTitle":"","hero":"","heroine":"","source":"IMDB"}
{"movieName":"","Leadactor":"","leadActress":"","movieTitle":"B","hero":"B1","heroine":"B2","source":"Netflix"}
{"movieName":"C","Leadactor":"C1","leadActress":"C2","movieTitle":"","hero":"","heroine":"","source":"IMDB"}
{"movieName":"D","Leadactor":"D1","leadActress":"D2","movieTitle":"","hero":"","heroine":"","source":"IMDB"}
{"movieName":"","Leadactor":"","leadActress":"","movieTitle":"E","hero":"E1","heroine":"E2","source":"Netflix"}

对于源 imdb,我感兴趣的字段与源 netflix 的感兴趣字段不同。我的最终目标是在前端的表格中显示它们。所以试图把我感兴趣的领域变成共同领域。

我想把它转换成:

{Name:"A",Actor"A1",Actress"A2","source":"IMDB"}
{Name:"B",Actor"B1",Actress"B2","source":"Netflix"}
{Name:"C",Actor"C1",Actress"C2","source":"IMDB"}
{Name:"D",Actor"D1",Actress"D2","source":"IMDB"}
{Name:"E",Actor"E1",Actress"E2","source":"Netflix"}

我想的解决方案是使用正则表达式删除所有具有空值的字段,然后将 movieName 和 movieTitle 替换为 Name。其他领域也是如此。

有没有更好的方法来处理这个问题。(我得到的 json 非常大。所以寻找超快速的方法来预处理它。)

最佳答案

我的建议是使用 gson 并在 JSON 字符串之上隐蔽到这样的 Map 中:

Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map =
gson.fromJson("{'key1':'foo','key2':'bar', 'key3', ''}", type);

一旦你得到你的 map ,然后 simlpy 迭代并删除所有具有空值的元素。之后,您可以从此 map 生成您的 JSON 字符串。

关于java - java中的json预处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com