0

I got JSON file from API but the content looks like this:

[
  "https:\/\/seekingalpha.com\/article\/4125943-apple-homepod-delay-big-deal?source=feed_symbol_AAPL"
]

Suppose the JSON Object above is named as json. Then I just convert the object to string using String() method.
strings = String(json)
When I changed it to String type, it seems to get unnecessary '\n' and whitespace in it.

"[\n  \"https:\\/\\/seekingalpha.com\\/article\\/4125943-apple-homepod-delay-big-deal?source=feed_symbol_AAPL\"\n]"

So it seems like the content of the JSON file is:

["\n""whitespace""whitespace""String""\n"]


When I changed it to String type, Swift just treats all the elements in it as a whole and wrapped it as a string. My question is, how to extract the String inside so it looks like:

"https:\\/\\/seekingalpha.com\\/article\\/4125943-apple-homepod-delay-big-deal?source=feed_symbol_AAPL\"

As I am not so familiar with Swift so how to extract String or JSON Object is not easy for me. Any hints or help will be appreciated.

Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
Mark Dong
  • 59
  • 2
  • 8
  • Show some code on how you are trying to convert it to a string. – Simon Nov 19 '17 at 03:50
  • By using `String()` method and I have updated above. – Mark Dong Nov 19 '17 at 03:57
  • Why would you convert your JSON to String in the first place? You don't need to do that. Just decode the JSON *data* using one of the usual tools: JSONSerialization, SwiftyJSON, JSON Decoder... – Eric Aya Nov 19 '17 at 12:57

3 Answers3

0
1. You will first have to convert JSON to Data
2. Convert data to string wrt to encoding    

    func jsonToString(jsonTOConvert: AnyObject){
        do {
            let data =  try JSONSerialization.data(withJSONObject: jsonTOConvert, options: JSONSerialization.WritingOptions.prettyPrinted) 
            let convertedString = String(data: data, encoding: String.Encoding.utf8) 
        } catch let myJSONError {
            print(myJSONError)
        }

}
Sumit Oberoi
  • 3,455
  • 2
  • 18
  • 18
0

You are asking that a String be created with the contents:

[
  "https:\/\/seekingalpha.com\/article\/4125943-apple-homepod-delay-big-deal?source=feed_symbol_AAPL"
]

The string object is doing exactly what you told it to — the thing you've asked it to represent begins with a square bracket, then there's a line break, then two spaces, etc, so the string contains a square bracket, then a line break, then two spaces, etc. There is no 'unnecessary' \n, there is only the \n you told it to put in.

If you obtained a JSON object then you need to parse it as JSON. JSONSerialization will do that job. What you've actually got is an array with a single item, which is a string. So JSONSerialization will return an array. The first item of that should be a string that is the seekingalpha.com URL.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

Swift 3,4 :

The given JSON format is Array of String.

if let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String]{

        let firstElement = json?.first ?? "Element Not Found!"

        print(firstElement)
}

Swift 4:

if let json = try? JSONDecoder().decode(Array<String>.self, from: data){

    let firstElement = json.first ?? "First Element Not Found!"
    print(firstElement)
}

Note: If your the Array contains more than one String. Here,urls is the class variable. i.e.,var urls = [String]()

Swift 3,4 :

if let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String]{

    if json != nil{

        self.urls = json!
    }

    print(self.urls)
}

Swift 4:

if let json = try? JSONDecoder().decode(Array<String>.self, from: data){

    self.urls = json
}
Rajamohan S
  • 7,229
  • 5
  • 36
  • 54