将Assistant V1 MessageResponse解析为JSON

时间:2018-11-02 12:27:30

标签: ios swift react-native-ios ibm-watson

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

如果您迫不及待要发布1.0版本(可能会在今年年底发布),则可以克隆swift-sdk存储库并为您正在使用的模型复制上面链接的PR I(基本上将类型从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)
}