我有一个看起来像这样的JSON响应,我想得到这个用户的所有帖子都有一个状态2:
user = {
"name": "John",
"posts": [
{
id: 1,
state: 1,
title: "Hello"
},
{
id: 2,
state: 1,
title: "Hi"
},
{
id: 3,
state: 2,
title: "World"
},
{
id: 4,
state: 3,
title: "Hey"
},
{
id: 5,
state: 2,
title: "Goodbye"
}
]
}
我该怎么做?
我想得到像我这样的答案
result = {
{
id: 3,
state: 2,
title: "World"
},
{
id: 5,
state: 2,
title: "Goodbye"
},
}
答案 0 :(得分:0)
post
user
内的,
不包含有效对象(每个对象后丢失filter()
)。
尝试使用数组的var user = {
"name": "John",
"posts": [
{
id: 1,
state: 1,
title: "Hello"
},
{
id: 2,
state: 1,
title: "Hi"
},
{
id: 3,
state: 2,
title: "World"
},
{
id: 4,
state: 3,
title: "Hey"
},
{
id: 5,
state: 2,
title: "Goodbye"
}
]
}
var state2Post = user.posts.filter(p => p.state == 2);
console.log(state2Post);
:
Gurmokhs-MBP:rest Gurmokh$ python
Python 2.7.12 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:43:17)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import psycopg2
if psycopg2.connect("dbname='postgres' user='postgres' host='localhost'"):
... print "connection made"
...
connection made
>>>