提取特定的JSON对象并独立显示它们(Extract Specific JSON Object and Display Them Independently)

编程入门 行业动态 更新时间:2024-10-11 11:22:03
提取特定的JSON对象并独立显示它们(Extract Specific JSON Object and Display Them Independently)

我的数据看起来像这样

{ "status": "success", "data": { "data1": { "serialNumber": "abc123", "version": "1.6" }, "data2": { "irrelvent": [ "irrelvent", "irrelvent" ] }, "data3": { "irrelevantLibs": { "irrelevantFiles": [ "irrelevant.jar", "irrelevant.jar", "irrelevant.jar" ] } }, "data4": { "configuration": "auth" } } }

我正在使用Jackson JSON处理器。 我需要在data对象下做的是将每个data(x)提取到它自己的数据中。

很难解释但我会尽力详细说明。 我的目的是使JSON数据对我们的客户更具可读性。 所以我试图通过将每个data(x)对象分成网站上的块/表来使JSON数据更友好。 因此data1将独立显示。 然后data2将独立显示,依此类推。 所以在这种情况下我有四个data(x)对象,我想将这四个对象存储到四个不同的Strings然后显示它们。 我的问题是如何让杰出处理器这样做?

不确定这是否有用但我当前处理JSON数据的JSON函数是这样的:

public static String stringify(Object o) { try { ObjectMapper mapper = new ObjectMapper(); DefaultPrettyPrinter printer = new DefaultPrettyPrinter(); printer.indentArraysWith(new Lf2SpacesIndenter()); return mapper.writer(printer).writeValueAsString(o); } catch (Exception e) { return null; } }

我很肯定我可以操纵处理器来分离这些数据,但我不知道从哪里开始。

编辑:嗯,因为杰克逊似乎是一个非常难以使用的解析器,如果我使用Javascript JSON解析器只提取data对象会更容易吗? 既然我已经让Jackson将JSON数据转换为String,那么使用Javascript会有效吗?

I have data that looks like this

{ "status": "success", "data": { "data1": { "serialNumber": "abc123", "version": "1.6" }, "data2": { "irrelvent": [ "irrelvent", "irrelvent" ] }, "data3": { "irrelevantLibs": { "irrelevantFiles": [ "irrelevant.jar", "irrelevant.jar", "irrelevant.jar" ] } }, "data4": { "configuration": "auth" } } }

I am using the Jackson JSON Processor. What I need to do under the data object is to extract each data(x) into it's own data.

Hard to explain but I will try to be detailed. My intent is to make the JSON data more readable for our customers. So I'm trying to make the JSON data more friendly by splitting each data(x) object into blocks/tables on a website. So data1 will be displayed independently. Then data2 will be displayed independently and so on. So in this case I have four data(x) objects and I want to store those four objects into four different Strings and display them however I want. My problem is how do I get the Jackson Processor to do this?

Not sure if this is helpful but my current JSON function to process the JSON data is this:

public static String stringify(Object o) { try { ObjectMapper mapper = new ObjectMapper(); DefaultPrettyPrinter printer = new DefaultPrettyPrinter(); printer.indentArraysWith(new Lf2SpacesIndenter()); return mapper.writer(printer).writeValueAsString(o); } catch (Exception e) { return null; } }

I'm positive that I can manipulate the processor to get those data separated but I don't know where to start.

EDIT: Hmm, since Jackson seems to be a pretty difficult parser to work with, would it be easier if I used the Javascript JSON parser to extract only the data objects? Since I already have Jackson convert the JSON data into a String, using Javascript would work?

最满意答案

“嗯,因为Jackson似乎是一个非常难以使用的解析器,如果我使用Javascript JSON解析器只提取数据对象会更容易吗?”

看到你正在使用Java,你考虑过使用Sling吗? http://sling.apache.org/它是一个外部库,它允许您非常迅速地解析和探索JSON数据结构,而且最重要的是干净利落。

So I was able to figure it out after really digging into the documentation. I had to use JsonNode in order to extract what I wanted. Note that the variable appName is just there for me to easily display the string data(x) while iteNode is just all elements under data(x)

public static List<String> jsonSplit(String o) throws JsonParseException, JsonMappingException, IOException { List<String> list = new ArrayList<String>(); ObjectMapper mapper = new ObjectMapper(); DefaultPrettyPrinter printer = new DefaultPrettyPrinter(); printer.indentArraysWith(new Lf2SpacesIndenter()); JsonNode rootNode = mapper.readValue(o, JsonNode.class); Iterator<String> appName = rootNode.get("data").getFieldNames(); Iterator<JsonNode> iteNode = rootNode.get("data").getElements(); while (iteNode.hasNext()){ list.add(appName.next()); list.add(mapper.writer(printer).writeValueAsString(iteNode.next())); } return list; }

更多推荐

本文发布于:2023-07-29 03:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1311984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   独立   Extract   JSON   Display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!