从JsonNode获取fieldname(Get fieldname from JsonNode)
我正在用Jackson解析一些json。 这个json的片段如下:
}, "1/1/0": { "event": "status", "name": "Alarm Status", "type": "Alarm", "location": "Hall" }, "1/1/1": { "event": "status", "name": "Smoke Alarm Status", "type": "Alarm", "location": "Hall" },
我很高兴能够为每个'对象'提取数据,但我很难获得每个对象的'名称'。 在上面的例子中,这将是'1/1/0'和'1/1/1'。 我这样做的方式如下:
final JsonNode node = parser.parseToNode(configJson); final JsonNode sensorsChild = node.get("sensors"); for (JsonNode root : sensorsChild) { final String event = root.get("event").asText(); final String name = root.get("name").asText(); final String type = root.get("type").asText(); final String location = root.get("location").asText(); }
我想要for循环中的另一行,如下所示:
final String id = root.getNodeFieldName();
这将分别产生'1/1/0'和'1/1/1'。 谢谢
I am using Jackson to parse some json. An snippet from this json is as follows:
}, "1/1/0": { "event": "status", "name": "Alarm Status", "type": "Alarm", "location": "Hall" }, "1/1/1": { "event": "status", "name": "Smoke Alarm Status", "type": "Alarm", "location": "Hall" },
I've happily managed to extract the data for each 'object', but I am struggling to obtain the 'name' for each object. In the example above, this would be '1/1/0' and '1/1/1'. I do so in the following way:
final JsonNode node = parser.parseToNode(configJson); final JsonNode sensorsChild = node.get("sensors"); for (JsonNode root : sensorsChild) { final String event = root.get("event").asText(); final String name = root.get("name").asText(); final String type = root.get("type").asText(); final String location = root.get("location").asText(); }
I want another line in the the for loop which is something like:
final String id = root.getNodeFieldName();
which would yield '1/1/0' and '1/1/1' respectively. Thanks
原文:https://stackoverflow.com/questions/40365334
最满意答案
只需在列表解析中使用嵌套循环:
sent = [word for line in f for word in line.lower().split()]
这种方法有一些替代方法,例如使用
itertools.chain.from_iterable()
,但我认为在这种情况下嵌套循环更容易。Just use a nested loop inside the list comprehension:
sent = [word for line in f for word in line.lower().split()]
There are some alternatives to this approach, for example using
itertools.chain.from_iterable()
, but I think the nested loop is much easier in this case.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您正在使用line.split()将每一行分成正确的单词列表,但您没有遍历刚刚创建的名为words的新列表。 您将列表words作为对象与lst的内容进行比较,然后将words作为对象附加到lst 。 这会导致lst成为列表列表,正如您在接收到的结果中所示。 为了实现您正在寻找的单词数组,您必须通过words进行迭代并单独添加每个单词,只要它不在lst : for word in words: if word not in lst: lst.append(word) 编辑:找到关于同一 ...
-
这是一个有效的解决方案。 我没有照顾打印。 结果是Map - > Word,Array。 其中Array包含从0开始索引的每个句子中的Word计数。在O(N)时间内运行。 在这里玩: https : //repl.it/Bg6D List
- > sort = new ArrayList<>();
Map
> res = new HashMap<>(); // split by sentence for ... -
如何检查列表中的单词是否包含在另一个列表中的句子中?(How do I check if words in a list are contained in sentences in another list?)[2023-09-04]
你可能正在寻找。 结果是所有未经过滤的字符串。 这样,您还可以捕获包含以点或逗号结尾的垃圾表达式的字符串。 import re import itertools # All of the strings z = ['a privacy policy', 'there are many standard challenges that face every business'] junk_terms = ['privacy policy', 'cookie policy', 'copyright'] # B ... -
Javascript提取不包含列表中任何单词的句子(Javascript Extract sentences not containing any words from a list)[2023-05-29]
使用第二个数组生成正则表达式,并使用RegExp#test方法检查模式匹配。 var extract = function(list1, list2) { // object for storing the result, do it as you want var res = { contains : [], notContains : [] }; // generate regex using the second list strings // in a way ... -
你在第二个脚本中遇到过几个问题: i是'the dog hates you'而在第一个脚本参数是['a frog ate the dog'] - >一个是字符串,第二个是列表。 word_list = word.split()用循环内部的这一行来实例化每次迭代的word_list ,而不是像我在代码示例中所写的那样使用append函数。 向字符串发送字符串时,需要在字符串循环前分割字符串。 尝试这个: def create_list_of_words(str_sentence): sentence ...
-
如果你可以建立一个包含所有单词的字符串,像(Turin|Milan)这样的list_of_words搜索,你可以在下面进行正则表达式匹配: ^.*\b(Turin|Milan)\b.*$ 此外,我们可以避免这两个for循环,如本答案中所述 。 if you could build a string with all the words to search from list_of_words like (Turin|Milan) , you could do a regex match on: ^.*\b ...
-
你需要str.split()把每个字符串分成单词: word_list = [word for line in sentence for word in line.split()] You need str.split() to split each string into words: word_list = [word for line in sentence for word in line.split()]
-
只需在列表解析中使用嵌套循环: sent = [word for line in f for word in line.lower().split()] 这种方法有一些替代方法,例如使用itertools.chain.from_iterable() ,但我认为在这种情况下嵌套循环更容易。 Just use a nested loop inside the list comprehension: sent = [word for line in f for word in line.lower().spli ...