代码之家  ›  专栏  ›  技术社区  ›  Johannes N

如何解析这个包含函数的复杂JSON对象

  •  0
  • Johannes N  · 技术社区  · 6 年前

    我得到了这个代码示例,想知道适合的MOJO(JSON对象)是什么样子的?它由一个复杂的JSON结构数组组成。

    {
        'inputContext': undefined,
        'intents': {
            //Default Welcome Intent
            'LaunchRequest': async (conv: AdfConversation) => {
    
            },
            'ExitIntent': async (conv: AdfConversation) => {
    
            },
            'DefaultFallbackIntent': async (conv: AdfConversation) => {
    
            },
            'Unhandled': (conv: AdfConversation) => {
    
            },
        },
    },
    {
        'inputContext': ConversationContexts.WELCOME,
        'intents': {
            'BargainIntent': async (conv: AdfConversation) => {
                conv.setContext(ConversationContexts.BARGAIN)
                conv.ask('Du hast dich für das Schnäppchen entschieden.', ':tell')
            },
            'PodcastIntent': async (conv: AdfConversation) => {
                conv.setContext(ConversationContexts.PODCAST)
                if(conv.type){
                    conv._alexaContext!.emitWithState('PodcastIntent',ConversationContexts.PODCAST)
                }
            },     
        },
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Daniel Hilgarth Richard    6 年前

    这不是JSON。它是实际数组对象的一部分。只需分配:

    const a = [{
        'inputContext': undefined,
        'intents': {
            //Default Welcome Intent
            'LaunchRequest': async (conv: AdfConversation) => {
    
            },
            'ExitIntent': async (conv: AdfConversation) => {
    
            },
            'DefaultFallbackIntent': async (conv: AdfConversation) => {
    
            },
            'Unhandled': (conv: AdfConversation) => {
    
            },
        },
    },
    {
        'inputContext': ConversationContexts.WELCOME,
        'intents': {
            'BargainIntent': async (conv: AdfConversation) => {
                conv.setContext(ConversationContexts.BARGAIN)
                conv.ask('Du hast dich für das Schnäppchen entschieden.', ':tell')
            },
            'PodcastIntent': async (conv: AdfConversation) => {
                conv.setContext(ConversationContexts.PODCAST)
                if(conv.type){
                    conv._alexaContext!.emitWithState('PodcastIntent',ConversationContexts.PODCAST)
                }
            },     
        },
    }];