在python3中解析Bloomberg API对JSON的响应

时间:2019-05-16 10:22:16

标签: json python-3.x bloomberg

如何将使用(python3和flask)中的Bloomberg API返回的响应消息转换为JSON

here is a response example:

ReferenceDataResponse = {
    securityData[] = {
        securityData = {
            security = "FB EQUITY"
            eidData[] = {
            }
            fieldExceptions[] = {
            }
            sequenceNumber = 0
            fieldData = {
                PX_LAST = 186.270000
                VOLUME = 16746904.000000
            }
        }
        securityData = {
            security = "IBM EQUITY"
            eidData[] = {
            }
            fieldExceptions[] = {
            }
            sequenceNumber = 1
            fieldData = {
                PX_LAST = 134.400000
                VOLUME = 2551009.000000
            }
        }
    }
}

用即将来临的代码处理它:

if str(msg.messageType()) == "ReferenceDataResponse":

    securities = msg.getElement('securityData')
    securities_count = securities.numValues()

    for i in range(securities_count):
        security = securities.getValueAsElement(i)
        ticker = security.getElementAsString('security')

        if (security.hasElement('fieldData')):
            fields = security.getElement('fieldData')
            fields_count = fields.numElements()
            for j in range (fields_count):

                security_dict = None
                field = fields.getElement(j)

                f_name = field.name()
                f_value = field.getValueAsString()

                security_dict = {"ticker":ticker ,"f_name":f_name , "f_value":f_value}
                bloom_data.append(security_dict)

给我(名称类型的对象不可JSON序列化) 现在,我无法访问名称对象以到达字段的名称 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

经过大量搜索,我发现此文档非常有用,可作为使用Bloomberg api处理响应的模式...。

这是链接==> api schema

使用python3处理respnse的示例:

bloom_data = []
if str(msg.messageType()) == "ReferenceDataResponse":

    securities = msg.getElement('securityData')
    securities_count = securities.numValues()

    for i in range(securities_count):
        security = securities.getValueAsElement(i)
        ticker = security.getElementAsString('security')

        if (security.hasElement('fieldData')):
            fields = security.getElement('fieldData')
            fields_count = fields.numElements()
            for j in range (fields_count):

                security_dict = None
                field = fields.getElement(j)

                f_name = field.name()
                f_value = field.getValueAsString()

                security_dict = {"ticker":ticker ,"f_name":str(f_name) , "f_value":f_value}
                bloom_data.append(security_dict)