将 Assistant V1 MessageResponse 解析为 JSON

Parse the Assistant V1 MessageResponse into JSON

我正在使用 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"}}

您是否尝试过使用 Watson 的 Swift SDK? https://github.com/watson-developer-cloud/swift-sdk. You can see the example of using Assistant using Watson swift SDK here: https://github.com/watson-developer-cloud/swift-sdk/tree/master/Source/AssistantV1.

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

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

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