如何在给定字符串的情况下返回json路径?

时间:2017-05-29 23:53:56

标签: python json python-2.7 list parsing

我在这里面临一个问题,因为我必须收到一个json,其中包含一个字符串,其中包含另一个json中某些数据的路径。

  1. 包含一些数据的Json

    json1 = {
        "Items": [{
                      "building": "buildingA",
                      "y": [1, 2, 3],
                      "yl": "A,B,C",
                      "xl": "Data",
                      "x": [1, 2, 3]
                  },
                  {
                      "y": [4, 5, 6],
                      "x": [1, 2, 3],
                      "predio": "BuildingB",
                      "yl": "D,E,F",
                      "xl": "Data"
                  }]
    }
    
  2. 以及包含所需值路径的json:

    json2 = {
        "y": "y//1",
    }
    
  3. 我尝试使用此代码来解决问题:

    def size2(a,i):
     x=a.split('//')
     y=len(x)
     if y ==1:
        return i[x[0]]
     elif y==2:
        return i[x[0]][x[1]]
     elif y==3:
        return i[x[0]][x[1]][x[2]]
    
    y=json2['y']
    for i in json1['Items']:
     print(i['y'][1])        #this way works
     print(size2(y,i))       #this way fails
    

    错误消息是:

    TypeERROR: list indices must be integers, not str
    

    有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

正如评论中已经指出的那样,只需在索引序列之前将索引转换为整数:

def size2(a,i):
    x = a.split('//')
    obj = i
    for part in x:  # using a loop you don't need to special case based on the length
        try:
            # Try to get it without converting to integer, for example to access dictionaries
            obj = obj[part]
        except TypeError:
            # Fallback to integer indexing
            obj = obj[int(part)]
    return obj

答案 1 :(得分:2)

你可以这样做,假设任何由所有数字字符组成的路径组件是一个整数序列索引:

def size2(y, i):
     obj = i
     for comp in ((int(z) if z.isdigit() else z) for z in y.split('//')):
        obj = obj[comp]
     return obj

y = json2['y']
for i in json1['Items']:
    print(size2(y, i))  # this way now works

使用内置的reduce()函数可以使size2()函数更加简洁:

def size2(y, i):
    return reduce(lambda obj, v: obj[int(v) if v.isdigit() else v], y.split('//'), i)