0
votes

All was working fine before but now Getting error like

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

My JSON parsing code is like

 func JSONParseArray(jsonString: String) -> [AnyObject] {
            if let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) {
                if let array = (try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)))  as? [AnyObject] {
                    return array
                }
            }
            print(strings)
            return [AnyObject]()
        }

what can be issue? or what I am doing wrong?

My JSOn String value is

http://test.domain.in/transfer/jsp/wsCall.jsp?dataFormat=JSON&requestData={"requestId":"1447417335364","requestType":"LOGIN","channelId":"MOBILE","clientId":"RG","groupId”:”Test”,”loginId":"[email protected]","password":"cFzc3dvcmRAMA==","noofAttempts":"1","ipAddress":"8ADCD445-A4F6-49EA-A344-35313A0AD","key":"MzQM3OTA4NDgxMjY4bWlsbGVy"}

2
What is the value of jsonString ? - Midhun MP
@Midhun I have updated my question - New iOS Dev
Your jsonstring is not a valid one. This part http://test.domain.in/transfer/jsp/wsCall.jsp?dataFormat=JSON&requestData= causes the issue. - Midhun MP
@Midhun what is the issue with my string? can plz explain so that i can work on it..because it was working fine before - New iOS Dev
http://test.domain.in/transfer/jsp/wsCall.jsp?dataFormat=JSON&requestData= should not be part of your jsonString. You'll have to figure out why is it coming. - saurabh

2 Answers

0
votes
request.setValue("text/json", forKey: "Content-Type")
0
votes

It seems the first letter of your response is h, the second is t, the third is t, the fourth is p. That's not valid JSON.

BTW. Never, ever, pass a string to a method that is supposed to parse JSON. Someone along the line sent NSData to you. Pass that NSData on to the JSON parser unchanged. That avoids wasting memory, wasting time, wasting battery power, and having unnecessary bugs.

BTW. For a method that can fail it is incredibly bad style to return anything other than an optional. Especially since you actually return an empty array in case of errors, which cannot be distinguished from an input of [] which gets parsed correctly and returns an empty error. So you are losing all error checking.