-3

Here is my string and I want to remove some characters from string. My code is below

"{  \"userpass\" : \"\",  \"apikey\" : \"=\",  \"deviceid\" : \"\",  \"username\" : \"\"}"

and I want to convert into this format.

{ 
    "userpass" : "",
    "username" : "",
    "deviceid" : "",
    "apikey" : "="
}

How to remove \ from string to make JSON string proper?

halfer
  • 19,824
  • 17
  • 99
  • 186
Krutarth Patel
  • 3,407
  • 6
  • 27
  • 54

4 Answers4

1

This is json in String response if you want to get dictionary from that try this, Here str is your string

let str = "{  \"userpass\" : \"\",  \"apikey\" : \"=\",  \"deviceid\" : \"\",  \"username\" : \"\"}"
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
do {
    let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
    print(dic)
}
catch let e as NSError {
    print(e.localizedDescription)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

If you see in debug monitor po myString it will be { "userpass" : "", "apikey" : "=", "deviceid" : "", "username" : ""}, this slash just in code not in program

RrrangeTop
  • 312
  • 1
  • 10
-1

In objective-c

NSString *stringWithoutSpaces = [yourString 
   stringByReplacingOccurrencesOfString:@" " withString:@""];
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
remyr3my
  • 768
  • 1
  • 6
  • 19
-1
All special characters with in string must use extra \ (escape sequence) to recognize the actual value    

 [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
    or string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

hope you will be clear
Nattudurai
  • 856
  • 7
  • 9
  • [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""]; – Nattudurai Jul 13 '16 at 11:39
  • r u still facing the same issue? [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""] – Nattudurai Jul 13 '16 at 12:00
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – HiDeoo Jul 13 '16 at 12:44
  • Yes bcz of all spl characters with in string must use extra \ (escape sequence) to recognize the actual value. – Nattudurai Jul 13 '16 at 17:09