-1

I use the following code

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response 
                                                     options:NSJSONReadingAllowFragments                                                           
                                                       error:&err];

first I want to add 2 options NSJSONReadingAllowFragments, and NSJSONReadingMutableContainers

if I used only NSJSONReadingMutableContainers the code will crash

now if the response is just string true or false , how to compare the value of NSArray to string "false" for example

I cannot say for example [jsonArray objectatindex:0]

any idea how to make my code intellegent enough such that if only the response is array of object or only string

Sulthan
  • 128,090
  • 22
  • 218
  • 270
AMH
  • 6,363
  • 27
  • 84
  • 135
  • You can check length of array for this. If length is 1 then again check value of array at index 0 for true/false. – aks.knit1108 Feb 16 '13 at 13:08
  • I cannot check for count , it will crash too – AMH Feb 16 '13 at 13:11
  • First check if `jsonArray` is nil. If it is check the `err`. Chances are the `response` is invalid, NSLog `response` and insure it is valid json. Basic debugging, check the inputs. – zaph Feb 16 '13 at 13:16
  • it's not nil , it's just contain the false string , but am not sure how to check it , I cannot say objectatindexof , or count , .... – AMH Feb 16 '13 at 13:19
  • 1
    Check `jsonArray` with `NSLog`. To find the class of `jsonArray`: `NSLog(@"class type: %@", NSStringFromClass([jsonArray class]);` – zaph Feb 16 '13 at 13:22
  • Take a look on http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization – aks.knit1108 Feb 16 '13 at 13:24
  • As I told in my example first check class type and if it is NSString type, then it means your string is true or false. – aks.knit1108 Feb 16 '13 at 13:27
  • `NSJSONReadingAllowFragments` is not a valid option for `JSONObjectWithData` when you expect an `NSArray`. Apple docs: NSJSONReadingAllowFragments Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary. – zaph Feb 16 '13 at 13:27
  • @Zaph the class is class type: __NSCFString how to extract the string within it , there is no isequaltostring – AMH Feb 16 '13 at 13:36
  • @Zaph it's part of options enum NSJSONReadingAllowFragments – AMH Feb 16 '13 at 13:36
  • `NSJSONReadingAllowFragments` is not valid for a return type of `NSArray` or `NSDictionary`. – zaph Feb 16 '13 at 13:48

2 Answers2

1

Before working on response check Class type.

id jsonObject = [NSJSONSerialization JSONObjectWithData:response 
                                                 options:NSJSONReadingAllowFragments                                                           
                                                   error:&err];

if ([jsonObject isKindOfClass:[NSArray class]]) {
    // Do stuff for array.
}
else if ([jsonObject isKindOfClass:[NSDictionary class]]) {
   // Do stuff for dictionary
}

else if ([jsonObject isKindOfClass:[NSString class]]) {
   // Do stuff for string
}
aks.knit1108
  • 1,305
  • 9
  • 20
  • what do u mean by the jsonData , is it the jsonArray , also how to add multiple options – AMH Feb 16 '13 at 13:26
  • see my edit. jsonArray is like jsonObject and I have change djsonData with response – aks.knit1108 Feb 16 '13 at 13:35
  • it's type of __NSCFString how to extract the data of it I mean I cannot say isequal to string or any other data – AMH Feb 16 '13 at 13:39
  • By placing above code in which block your control goes, during debugging check it and tell me – aks.knit1108 Feb 16 '13 at 13:44
  • thanks a lot it work very well , I will test against dictionary and Nssarray – AMH Feb 16 '13 at 13:55
  • am sorry but will the option NSJSONReadingAllowFragments will read dictionary or nssarray – AMH Feb 16 '13 at 14:00
  • If you are facing problem then you can use NilOption for the same. Because I am not sure about NSJSONReadingAllowFragments. Because I never tested it on my end. – aks.knit1108 Feb 16 '13 at 14:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24621/discussion-between-best-coder-and-amh) – aks.knit1108 Feb 16 '13 at 14:23
0

The returned JSON is a fragment that is an NSString, count is not valid for strings. Change the return type:

NSString *jsonString = [NSJSONSerialization JSONObjectWithData:response 
                                                     options:NSJSONReadingAllowFragments                                                           
                                                       error:&err];

NSLog(@"jsonArray: %@, length: %d", jsonString, jsonString.length);

Then do whatever you want with the string.

Generally JSON is an array or a dictionary, in this case it is a string, possibly the JSON is not correctly formatted.

zaph
  • 111,848
  • 21
  • 189
  • 228