1

I'm Using Swift2.0+, SwiftyJSON, Alamofire

I Got the Value let stringJSON:JSON = ["a1","a2","a3"] from Server

But If I Check stringJSON[0],Then it's null

When I debugPrint(stringJSON), Then it's ["a1","a2","a3"]

How Can I got the value stringJSON[0] //"a1" ?

Do I Have To Convert From JSON To Another?

2
  • Are you sure stringJSON it's a array and not a simple String? Maybe something like this "["a1","a2","a3"]" and not this ["a1","a2","a3"]? Commented Feb 26, 2016 at 13:53
  • I checked. debugPrint(stringJSON is JSON) is true and debugPrint(stringJSON) is ["a1","a2","a3"] Commented Feb 26, 2016 at 13:56

4 Answers 4

2

The correct syntax is

let stringJSON:JSON = ["a1","a2","a3"]

if let firstValue = stringJSON.array?.first {
    print(firstValue) // a1
}

Update

Since the value actually contains this string "[\"a1\/a1\",\"a2\/a2\",\"a3\"]" and you cannot fix this on the server side here it is a workaround.

if let words = stringJSON.string?.characters.dropFirst().dropLast().split(",").map(String.init) {
    let word = String(words[0].characters.dropFirst().dropLast())
    print(word) // a1
}
Sign up to request clarification or add additional context in comments.

8 Comments

The Code Works Perfect. But In My Code Value stringJSON.array returns nil.
@Yoohogyun: So it's not an array. You have something else inside your JSON. If you try my code (where stringJSON actually contains an array) in Playground it will work.
Yes I Tried That. That Working On. But When I Got the Data From Server(Using Alamofire) Then, stringJSON.array Not Working On. Any Thing else?
You must check the server. It's not sending you an array. Please let me see your full JSON in plain text.
Edited Answer works Perfect! Thanks for Your concern
|
1
if let x = stringJSON[0].string{
print(x)
}

4 Comments

compile Error. Error Message here. Value of type 'JSON' has no member 'String'
@Yoohogyun It's .string and everything you need to know is already in the examples on GitHub: github.com/SwiftyJSON/SwiftyJSON#subscript
@EricD gah! I was so close
i've read them. but I Mean The JSON To JSON Array. The Document Has Only NSArray To JSON.
1

Since the server is not returning an Array, but a String, you need to convert that into an Array of Strings like this:

let string = stringJSON.string
let array = string.stringByReplacingOccurrencesOfString("[", withString: "")
    .stringByReplacingOccurrencesOfString("]", withString: "")
    .stringByReplacingOccurrencesOfString("\"", withString: "")
    .componentsSeparatedByString(",")

Comments

0

stringJSONIf it is a string type, you can like this to solve:

extension String {

var parseJSONString: AnyObject? {
    var any: AnyObject?
    let data = self.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    do{
        any = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)
    }catch let error as NSError{
        print("error: \(error)")
    }
    return any
}

and use it like this:

let strArr = stringJSON.stringvalue.parseJSONString as! Array<String>

1 Comment

First, you want to make sure it is a type string array,like this: "[\"a1\/a1\",\"a2\/a2\",\"a3\"]" The need to parse

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.