4

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

3
  • 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 Commented Sep 17, 2012 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.. Commented Sep 17, 2012 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.. Commented Sep 17, 2012 at 10:39

3 Answers 3

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

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

:)

1 Comment

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

Comments

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.