简体   繁体   中英

Parse the Assistant V1 MessageResponse into JSON

I'm creating a chatbot in react-native using IBM Watson Assistant. Since there was no good library I decided to create a bridge myself.

So the problem i'm facing here is in iOS , I'm able to get the response but i want it to be in json format. I'm using swift 4.

The format I receive:

(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: [:])

The format I want:

{"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"}}

Have you tried using Watson's 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 .

The Swift SDK will support 2-way conversion between objects and raw JSON once version 1.0 is released (by the end of this year). Here is the PR that will implement the functionality to support changing response objects back into JSON: https://github.com/watson-developer-cloud/swift-sdk/pull/910 .

If you cannot wait for version 1.0 to be released (which will likely be at the end of the year), then you can clone the swift-sdk repo and reproduce the PR I linked above for the models you are using (basically changing the type from Decodable to Codable ). For your MessageResponse example, you would make these changes . Then, in your request completion handler, do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM