-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?

5
  • 2
    you want remove \n or `\` Commented Jul 13, 2016 at 11:29
  • Please Check my updated question Commented Jul 13, 2016 at 11:32
  • 4
    This string is pretty-printed JSON. You should not try to decode it by doing character replacement/parsing but by using proper JSON methods. Commented Jul 13, 2016 at 11:32
  • my actual requirement is to pass json dictionary to server. Commented Jul 13, 2016 at 11:33
  • ok.than how to should i post this json to server using alamofire Commented Jul 13, 2016 at 11:34

4 Answers 4

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)
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Nirav. Requirement is i have to pass dictionary instead of array as parameter to server. how to do that.can you please explain?
Check my edited answer. You will get dictionary.
Yes.i am getting dictionary.now question is how to pass it to server using alamofire?
You can find that on alamofire document
You can follow this link or this link
0

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

Comments

-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

4 Comments

[string stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
r u still facing the same issue? [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""]
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.
Yes bcz of all spl characters with in string must use extra \ (escape sequence) to recognize the actual value.
-1

In objective-c

NSString *stringWithoutSpaces = [yourString 
   stringByReplacingOccurrencesOfString:@" " withString:@""];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.