简体   繁体   English

在python中使用jsonpickle解析多个子属性

[英]Using jsonpickle in python to parse multiple child attributes

I'm parsing JSON that looks like this: 我正在解析如下所示的JSON:

{
    "attr1": true,
    "attr2": "foo",
    "attr3": 7,
    "attr4": [
        {
        "someattr1": "foo",
        "someattr2": "bar"
        },
        {
        "someattr1": "foo",
        "someattr2": "bar"
        },
        ],
    "attr6": false
}

How would I go about getting the 2nd attr4's someattr1 using jsonpickle? 我将如何使用jsonpickle获取第二个attr4的someattr1? Kinda got me lost. Kinda让我迷路了。 Thanks in advance. 提前致谢。

How would I go about getting the 2nd attr4's someattr1 using jsonpickle? 我将如何使用jsonpickle获取第二个attr4的someattr1?

Please note that your json object has an extra comma before the closing square bracket that will make the parser fail. 请注意,您的json对象在右方括号前有一个逗号,这会使解析器失败。 Once removed that, you can: 删除后,您可以:

import jsonpickle as jp

json = '''
{
    "attr1": true,
    "attr2": "fooA",
    "attr3": 7,
    "attr4": [
        {
        "someattr1": "fooB",
        "someattr2": "barC"
        },
        {
        "someattr1": "fooD",
        "someattr2": "barE"
        }
        ],
    "attr6": false
} '''

print jp.decode(json)['attr4'][1]['someattr1']  #index == 1 → 2nd in the series!!

HTH! HTH!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM