How to create json Object with NSData in Objective C?

12,431

Solution 1

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
}

Solution 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)
    }

Solution 3

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...

:)

Share:
12,431
Deepak Pillai
Author by

Deepak Pillai

i'm new to iPhone (IOS 5) development. I have previous experience in PHP MySql about 2.5 years. Now i changed ma platform to iPhone (Obj C)

Updated on June 18, 2022

Comments

  • Deepak Pillai
    Deepak Pillai almost 2 years

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

    • Vimal Venugopalan
      Vimal Venugopalan over 11 years
      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
    • Deepak Pillai
      Deepak Pillai over 11 years
      @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
      Deepak Pillai over 11 years
      @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
    Deepak Pillai over 11 years
    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
    Deepak Pillai over 11 years
    Hi Moxy, am new to iPhone development and am little confused. can u show me the code to do the same..
  • Moxy
    Moxy over 11 years
    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?
  • Deepak Pillai
    Deepak Pillai over 11 years
    But when am printing the values of jsonDictionaryOrArray (NSLog(@"%@",jsonDictionaryOrArray);) it displays as nil. Do u know why??
  • Moxy
    Moxy over 11 years
    I edited my answer so that you can check what is the problem.