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 sendVimal Venugopalan– Vimal Venugopalan2012-09-17 10:32:24 +00:00Commented 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..Deepak Pillai– Deepak Pillai2012-09-17 10:37:46 +00:00Commented 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..Deepak Pillai– Deepak Pillai2012-09-17 10:39:34 +00:00Commented Sep 17, 2012 at 10:39
Add a comment
|
3 Answers
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
}
4 Comments
Deepak Pillai
Hi Moxy, am new to iPhone development and am little confused. can u show me the code to do the same..
Moxy
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
But when am printing the values of jsonDictionaryOrArray (NSLog(@"%@",jsonDictionaryOrArray);) it displays as nil. Do u know why??
Moxy
I edited my answer so that you can check what is the problem.
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
Deepak Pillai
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??
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)
}