Parse Bloomberg API ответ на JSON в python3 - PullRequest
2 голосов
/ 16 мая 2019

Как я могу преобразовать ответное сообщение, возвращенное из API Bloomberg в (python3 и flask), в 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)

дай мне (Объект типа Name не JSON-сериализуемый) Теперь я не могу получить доступ к имени объекта, чтобы получить имя поля любая помощь будет очень ценится

1 Ответ

3 голосов
/ 23 мая 2019

После долгих поисков я нашел этот документ, который очень полезен в качестве схемы для использования API Bloomberg для работы с ответом ....

Вот ссылка ==> API-схема

пример обращения с использованием python3:

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)
...