将Assistant V1 MessageResponse解析为JSON

问题描述 投票:1回答:2

我正在使用IBM Watson Assistant在react-native中创建一个聊天机器人。由于没有好的图书馆,我决定自己创建一座桥梁。

所以我面临的问题是在iOS中,我能够得到响应,但我希望它是json格式。我正在使用swift 4。

我收到的格式:

(input: Optional(AssistantV1.MessageInput(text: Stupid)), intents: [], entities: [], alternateIntents: nil, context: AssistantV1.Context(conversationID: Optional("2380129380139312kjh123"), system: Optional(AssistantV1.SystemResponse(additionalProperties: ["_node_output_map": RestKit.JSON.object(["Welcome": RestKit.JSON.object(["1": RestKit.JSON.array([RestKit.JSON.int(0), RestKit.JSON.int(0), RestKit.JSON.int(1), RestKit.JSON.int(2)])])]), "initialized": RestKit.JSON.boolean(true), "branch_exited": RestKit.JSON.boolean(true), "dialog_stack": RestKit.JSON.array([RestKit.JSON.object(["dialog_node": RestKit.JSON.string("root")])]), "dialog_turn_counter": RestKit.JSON.int(1), "dialog_request_counter": RestKit.JSON.int(1), "branch_exited_reason": RestKit.JSON.string("completed")])), additionalProperties: ["error_question": RestKit.JSON.string(""), "my_name": RestKit.JSON.string("username"), "my_email": RestKit.JSON.string("useremail"), "my_credentials": RestKit.JSON.object(["password": RestKit.JSON.string("3120834hbdi37dyd"), "user": RestKit.JSON.string("uwq9383ueh9833298e")]), "err_cnt": RestKit.JSON.int(0)]), output: AssistantV1.OutputData(logMessages: [], text: ["Sorry, I am constantly learning."], generic: Optional([AssistantV1.DialogRuntimeResponseGeneric(responseType: "image", text: nil, time: nil, typing: nil, source: Optional("someurl"), title: nil, description: nil, preference: nil, options: nil, messageToHumanAgent: nil, topic: nil, suggestions: nil), AssistantV1.DialogRuntimeResponseGeneric(responseType: "text", text: Optional("Sorry, I am constantly learning."), time: nil, typing: nil, source: nil, title: nil, description: nil, preference: nil, options: nil, messageToHumanAgent: nil, topic: nil, suggestions: nil)]), nodesVisited: Optional(["Welcome"]), nodesVisitedDetails: nil, additionalProperties: [:]), actions: nil, additionalProperties: [:])

我想要的格式:

{"entities":[],"output":{"generic":[{"source":"someurl","response_type":"image"},{"response_type":"text","text":"Sorry, I am constantly learning."}],"text":["Sorry, I am constantly learning."],"nodes_visited":["node_1_1535596064008"],"log_messages":[]},"intents":[{"intent":"Negative_Feedback","confidence":1}],"context":{"system":{"dialog_turn_counter":3,"initialized":true,"dialog_stack":[{"dialog_node":"root"}],"dialog_request_counter":3,"_node_output_map":{"node_2_1536025247756":{"0":[0]},"Welcome":{"1":[0,0,1,2]},"node_1_1535596064008":{"1":[0,0]}},"branch_exited":true,"branch_exited_reason":"completed"},"my_name":"username","err_cnt":0,"conversation_id":"13212230293","error_question":"","my_email":"useremail","my_credentials":{"user":"123192312b3283y12b129","password":"dadu8sdsadjb283"}},"input":{"text":"Stupid"}}
ios swift react-native-ios ibm-watson
2个回答
1
投票

一旦版本1.0发布(到今年年底),Swift SDK将支持对象和原始JSON之间的双向转换。这是PR将实现支持将响应对象更改回JSON的功能:https://github.com/watson-developer-cloud/swift-sdk/pull/910

如果您不能等待1.0版本发布(可能会在年底),那么您可以克隆swift-sdk repo并为您正在使用的模型重现上面链接的PR(基本上更改类型从DecodableCodable)。对于你的MessageResponse例子,你会做these changes。然后,在您的请求完成处理程序中,执行以下操作:

do {
    let data = try JSONEncoder().encode(messageResponse)
    if let rawJSON = String(data: data, encoding: .utf8) {
        print(rawJSON)
    }
} catch {
    print(error)
}

1
投票

您是否尝试过使用Watson的Swift SDK? https://github.com/watson-developer-cloud/swift-sdk。您可以在此处查看使用Watson swift SDK使用Assistant的示例:https://github.com/watson-developer-cloud/swift-sdk/tree/master/Source/AssistantV1

© www.soinside.com 2019 - 2024. All rights reserved.