Zecheng Li
Zecheng Li

Reputation: 53

convert alamofire json response to variable

I have a question that I already asked several times on stackoverflow and I have tried all of them, there is none of them worked. So I would love to summarize the question for another time, and try to describe it more precise.

I am building an app sending a picture to a python back end for the result of image recognition in xcode swift.

And I am using Alamofire to upload, here is the uploading part:

Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(imageData!, withName: "pic", fileName: "filename.png", mimeType: "image/png")

        }, to: "http:123456.com/image",
           method: .post,
           encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseString { response in
                    debugPrint(response)
                }

            case .failure(let encodingError):
                print(encodingError)
            }

And here is the json response I got from the server end:

[Response]: <NSHTTPURLResponse: 0x600000237de0> { URL: 1234/image } { Status Code: 200, Headers {
    "Content-Length" =     (
        348
    );
    "Content-Type" =     (
        "text/html; charset=utf-8"
    );
    Date =     (
        "Mon, 09 Apr 2018 20:59:30 GMT"
    );
    Server =     (
        "Werkzeug/0.12.2 Python/2.7.12"
    );
} }
[Data]: 348 bytes
[Result]: SUCCESS: {
 "prediction": [
  {
   "name": "marshmallow", 
   "value": 0.2800232470035553
  }, 
  {
   "name": "caesar salad", 
   "value": 0.090629942715168
  }, 
  {
   "name": "egg", 
   "value": 0.07480788230895996
  }, 
  {
   "name": "apple", 
   "value": 0.049235329031944275
  }, 
  {
   "name": "chickpea", 
   "value": 0.04692944884300232
  }
 ]
}
[Timeline]: Timeline: { "Request Start Time": 545000363.584, "Initial Response Time": 545000363.642, "Request Completed Time": 545000370.462, "Serialization Completed Time": 545000370.487, "Latency": 0.058 secs, "Request Duration": 6.879 secs, "Serialization Duration": 0.025 secs, "Total Duration": 6.903 secs }

So, the purpose I want to have, is just print the name of the first prediction's name.

like the output is

outcome is marshmallow with a value of 0.28

I have tried several ways:

First, I want to have a struct to store the name as string, value as double, and use a loop to parse it. But whatever I tried, I always get a "null" as output, or nothing.

Any suggestions on this? Thanks in advance.

Upvotes: 0

Views: 447

Answers (1)

JuanCarlosDgz
JuanCarlosDgz

Reputation: 41

try this:

upload.responseJSON { response in
if let result = response.result.value {
                        let json = result as! [String: Any]
                        if let predictionArray = json["prediction"] as? [[String: Any]],
                            let firstPrediction = predictionArray.first {
                            print(firstPrediction)
                        }
                        print(json)
                    }
                }

Upvotes: 1

Related Questions