我试图通过查询在我的JSON中获取一个元素。
我正在使用Groovy,Postgres 9.4和JSONB。
这是我的JSON
UIFont *postFont = [UIFont fontWithName:@"OpenSans" size:13.0];
NSString *postWithFont = [self.msgHtml stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}i{font-family: '%@'; font-size:%fpx;}</style>", postFont.fontName, postFont.pointSize, [postFont.fontName stringByAppendingString:@"-Italic"], postFont.pointSize]];
_mainText = [[NSMutableAttributedString alloc]
initWithData:[postWithFont dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
我想通过ID获取系列数组中的各个元素,我目前正在使用此查询
{
"id": "${ID}",
"team": {
"id": "123",
"name": "Shire Soldiers"
},
"playersContainer": {
"series": [
{
"id": "1",
"name": "Nick",
"teamName": "Shire Soldiers",
"ratings": [
1,
5,
6,
9
],
"assists": 17,
"manOfTheMatches": 20,
"cleanSheets": 1,
"data": [
3,
2,
3,
5,
6
],
"totalGoals": 19
},
{
"id": "2",
"name": "Pasty",
"teamName": "Shire Soldiers",
"ratings": [
6,
8,
9,
10
],
"assists": 25,
"manOfTheMatches": 32,
"cleanSheets": 2,
"data": [
3,
5,
7,
9,
10
],
"totalGoals": 24
}
]
}
}
然而,这让我回到了id为1和2的元素
以下是我的回复
select content->'playersContainer'->'series' from site_content
where content->'playersContainer'->'series' @> '[{"id":"1"}]';
谁能看到我哪里出错了?我在这里看到了其他一些问题,但他们对此没有帮助。
答案 0 :(得分:2)
content->'playersContainer'->'series'
是一个数组。如果要在数组中查找特定元素,请使用jsonb_array_elements()
。
select elem
from site_content,
lateral jsonb_array_elements(content->'playersContainer'->'series') elem
where elem @> '{"id":"1"}';