4

How to create json Object with NSData in Objective C. I'm having values in a NSData variable.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
Deepak Pillai
  • 105
  • 1
  • 3
  • 12
  • do you want to send the NSData as it is or get values from it and then create your JSON from it and then send – Vimal Venugopalan Sep 17 '12 at 10:32
  • @VakulSaini:- ys i tried and found a function NSJSONSerialization. but when i tried to insert NSData to it, i got an error.. Can u please help me to find out a better solution.. – Deepak Pillai Sep 17 '12 at 10:37
  • @VimalVenugopalan:- I already have data in a variable named encryptedData and its data type is NSData. Now i want to make a json object which holds the encryptedData value.. Do u have any suggestion.. – Deepak Pillai Sep 17 '12 at 10:39

3 Answers3

6

You can use it like this in iOS 5 (if you are sure of your json structure you can directly use NSArray or NSDictionary doing a cast)

NSError *jsonError;
id jsonDictionaryOrArray = [NSJSONSerialization JSONObjectWithData:myData options:NULL error:&jsonError];
if(jsonError) {
    // check the error description
    NSLog(@"json error : %@", [jsonError localizedDescription]);
} else {
    // use the jsonDictionaryOrArray
}
William Denniss
  • 16,089
  • 7
  • 81
  • 124
Moxy
  • 4,162
  • 2
  • 30
  • 49
  • Hi Moxy, am new to iPhone development and am little confused. can u show me the code to do the same.. – Deepak Pillai Sep 17 '12 at 10:36
  • This line of code should be enough to get your json object. What is it that you don't understand? Do you already know how your json is formatted? – Moxy Sep 17 '12 at 10:48
  • But when am printing the values of jsonDictionaryOrArray (NSLog(@"%@",jsonDictionaryOrArray);) it displays as nil. Do u know why?? – Deepak Pillai Sep 17 '12 at 10:53
  • I edited my answer so that you can check what is the problem. – Moxy Sep 17 '12 at 11:02
2

if you have a value in NSData object then you can convert it in NSString variable like bellow

NSString *response = [[NSString alloc] initWithData:receivedData
                                                encoding:NSUTF8StringEncoding];

Edited...

i am not sure what you want but i give you the json array from string like bellow..

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSError *error;
    SBJSON *json = [[SBJSON new] autorelease];
    NSArray *arrData = [json objectWithString:responseString error:&error];
    [responseString release];

you can get data in array

hope this help you mate...

:)

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • ys i tried to convert NSData to NSString. But it always return nul. Also am afraid to convert it bcos it may cause to data lose. Now the NSData variable hold IV, SALT and the CIPHERTEXT. I join these three values to a single variable named encryptedData. (NSData *encryptedData;). And now i want to create a json which can hold the encryptedData.. any help?? – Deepak Pillai Sep 17 '12 at 10:32
2
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"youur link"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
if([jsonObjects isKindOfClass:[NSArray class]]){
    //Is array
}else if([jsonObjects isKindOfClass:[NSDictionary class]]){
    //is dictionary
}else{
    //is something else
}

EDIT FOR SWIFT

do {
        if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>] {

        } else {
            print("bad json")
        }
    } catch let error as NSError {
        print(error)
    }
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40