0

I am getting output as string. But the value is in key & value

            var buffer = Array<UInt8>(repeating: 0, count: 4096)
        if ( aStream == inputStream){

            while (inputStream.hasBytesAvailable){
                let len = inputStream.read(&buffer, maxLength: buffer.count)
                if(len > 0){
                    let output  = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)

                    if (output != ""){
                        NSLog("server said: %@", output!)
                        receivingData = output as! String
                        delegate.socketReceiveData(responce: receivingData)
                    }

                }

            }
        }

data is coming in dictionary but the object is in string can’t able to covert. output = {“password": "12345", "method": "login", "mail_id": "[email protected]"}from server but output is string how to read value of method, password etc...

2
  • is output = also coming in the string or the string is {“password": "12345", "method": "login", "mail_id": "[email protected]"} Oct 26, 2016 at 5:05
  • the string is {“password": "12345", "method": "login", "mail_id": "[email protected]"}.
    – user4468952
    Oct 26, 2016 at 5:15

2 Answers 2

0

plz use this

let strf = "{\"name\":\"maan\"}"


        let data = strf.data(using: String.Encoding.utf8)

         do {
            guard let toddo = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] else
            {

                print("error trying to convert data to JSON")
                return
            }

            print(toddo);
        }
        catch {
            print(error)
        }
1
  • Getting this error Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}
    – user4468952
    Oct 26, 2016 at 5:45
0

This is a JSON string "{"password": "12345", "method": "login", "mail_id": "[email protected]"}"

And you can convert it to a dictionary as follow

let data = output!.data(using: String.Encoding.utf8, allowLossyConversion: false)

do {
  let json = try JSONSerialization.jsonObject(with: data!, options: [])
  let dict = json as? [String: Any]
  let method = dict?["method"]
  print(method)
}
catch {
  print(error)
}

You can also try jsonObject(with stream: InputStream, options opt: JSONSerialization.ReadingOptions = []) throws -> Any method and supply your input stream to it


Edit

Try this code as well

var buffer = Array<UInt8>(repeating: 0, count: 4096)
if ( aStream == inputStream){
  
  while (inputStream.hasBytesAvailable){
    let len = inputStream.read(&buffer, maxLength: buffer.count)
    if(len > 0){
      
      let data = Data(bytes: &buffer, count: buffer.count)
      
      do {
        let json = try JSONSerialization.jsonObject(with: data!, options: [])
        let dict = json as? [String: Any]
        let method = dict?["method"]
        print(method)
      }
      catch {
        print(error)
      }
      
//          if (output != ""){
//            NSLog("server said: %@", output!)
//            receivingData = output as! String
//            delegate.socketReceiveData(responce: receivingData)
//          }
      
    }
    
  }
}
6
  • Show this error Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}
    – user4468952
    Oct 26, 2016 at 5:20
  • Can you post the screenshot of the console for this output NSLog("server said: %@", output!). Seems like you've malformed json Oct 26, 2016 at 5:21
  • server said: {"method_status":"right","method_name":"login","id":"1","name":"testuser","phone":"2147483647","mail_id":"[email protected]","password":"12345","status":"1","created_at":"Fri Jun 24 2016 15:28:26 GMT+0530 (India Standard Time)"}
    – user4468952
    Oct 26, 2016 at 5:24
  • Dear you have some special invisible characters in the string you can put your whole string here and see jsoneditoronline.org Oct 26, 2016 at 5:28
  • Changed the acaii coding to utf8, please try that other wise directly convert bytes in nsdata and then see Oct 26, 2016 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.