1

I am trying to write a JSON parser in swift. I am writing functions for parsing different parts of JSON code. I wrote a string parser which detects a string from the JSON data, by checking the start with \" and if I meet with another \" it is separated and returned as a String but when I met with this JSON text:

{"gd$etag": "W\/\"D0QCQX4zfCp7I2A9XRZQFkw.\""}

the function I wrote failed in the above case since in the value part it has to recognise the whole as String while mine is working to collect only

W\/

Since I gave the condition as starting and ending with \" when I searched online I understood it is something in relation to regular expressions. So help me out to solve this!

5
  • 1
    Why reinvent the wheel? Use NSJSONSerialization (or 3rd party libs like SwiftyJSON). Commented Apr 25, 2015 at 14:42
  • @MartinR I really like the ObjectMapper library. But I think he's just wondering how to detect \"SAMPLE TEXT\" in a string. Commented Apr 25, 2015 at 15:29
  • I am just a noob to programming so I started doing as a project and get strong with fundas! And my main aim is to make it pure swift! Commented Apr 25, 2015 at 15:36
  • I am pretty sure that the question how to parse a string with nested escaped quotes with regular expressions has been answered before. – Apart from that, parsing JSON with regular expressions is probably a bad idea (think of strings in an array of dictionaries ...) Commented Apr 25, 2015 at 15:37
  • I just realised swift doesn't support regular expressions and I just found another way to sort this problem Commented Apr 29, 2015 at 15:37

2 Answers 2

1

Is this what you were looking for?

import Foundation

let str: NSString = "W\\/\\\"D0QCQX4zfCp7I2A9XRZQFkw.\\\""
let regex = "\\\\\".*\\\\\""

// Finds range that starts with \" and ends with \"
let range = str.rangeOfString(regex, options: .RegularExpressionSearch)
let match: NSString = str.substringWithRange(range)

//Removes the \" from the start and end.
let innerString = match.substringWithRange(NSMakeRange(2, match.length-4))
Sign up to request clarification or add additional context in comments.

2 Comments

my agenda is to keep the code pure swift and I realised that swift doesn't support regex and I just worked with something else to solve this
Yeah. I need to test out using regex with Emoji.
0
if first(jsonString) == "\"" {
        jsonString.removeAtIndex(jsonString.startIndex)
        for elem in jsonString {
            if elem == "\\" {
                s = 1
                parsedString.append(elem)
                jsonString.removeAtIndex(jsonString.startIndex)
                continue
            }
            if s == 1 && elem == "\""{
                parsedString.append(elem)
                jsonString.removeAtIndex(jsonString.startIndex)
                s = 0
                continue
            }
            else if elem == "\""{
                jsonString.removeAtIndex(jsonString.startIndex)
                break
            }
            parsedString.append(elem)
            jsonString.removeAtIndex(jsonString.startIndex)
            s = 0
        }

This code lets me solve my issue to keep it pure swift.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.