I am trying to send an NSMutableArray using custom URL. Below is the code to do send:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tempArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)jsonString,
NULL,
(CFStringRef)#"<>!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
I am able to send the encoded string in custom url and access it via [url query] followed by url decode. My problem is that I am able to get contents similar to what is in "jsonString" but afterwards I am not getting how to retrieve the array from it. Can someone guide to the right direction.
Thanks
Related
I have an XML file and I am converting the data into NSString using this code:-
NSString *strPath=[[NSBundle mainBundle] pathForResource:#"VISG" ofType:#"xml"];
NSData *data = [NSData dataWithContentsOfFile:strPath];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
The problem I am facing is I am getting the string with all the XMLValues but I am unable to convert that string into Dictionary. I have used XMLReader but it always returns me nil . I have tried the below two methods for converting the string which I ma getting from NSdata but it always throws nil
dict = [XMLReader dictionaryForXMLString:string
options:XMLReaderOptionsProcessNamespaces
error:&error];
dict = [XMLReader dictionaryForXMLString:string error:nil]
Can anyone please tell me what is the problem. Or can you please tell me some more libraries other than XMLReader for parsing the XMLfile.
NSString *urlString = [NSString
stringWithFormat:#"http://192.168.1.15/abc/service.asmx?op=GetCenter"];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSError *error;
NSMutableArray *json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",[json description]);
I have try this but it returns null value insted of call function.
Program ended with exit code: 9(lldb)
How to recognized if webserive is called or not or how call that function?
If you could provide a bit more information as to what you mean by the function is not called, that would be very helpful, is there any stack trace or other information shown when it crashes?
I would troubleshoot this by stepping through each line of code in the debugger to make sure you are getting data back and then as suggested to print out the error from the serialization function.
It also might be worth using these methods:
NSString* newStr = [NSString stringWithContentsOfURL:url encoding: NSUTF8StringEncoding error:&error];
or
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
to get a string from your URL or data once you have downloaded it and then print out or view the contents.
Longer term you might want to think about using something like AFNetworking to do your networking calls as what you have is synchronous and ideally, you should be doing all networking asynchronously.
I have to store an NSArray (which contains NSString and BOOL) into a KeychainItemWrapper to re use it in another ViewController, and to keep it in memory even if the app is closed.
I've already see at this question but it won't help me, because I can't find the SBJsonWriter files.
Can anyone could help me please ?
Thanks a lots.
Have a good day !
SBJsonWriter is a 3rd party JSON library popular years back, now iOS has this built in.
Serialize the data as JSON using the native NSJSONSerialization, and then write it to the keychain (assuming kSecValueData, which is encrypted):
NSArray* array = ...;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:array options:0 error:nil];
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[keychainItem setObject:jsonString forKey:(__bridge id)kSecValueData];
To read the data back to an NSArray:
NSString* jsonString = [keychainItem objectForKey:(__bridge id)kSecValueData];
NSArray* array = nil;
if(jsonString.length > 0)
{
id jsonObject = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
// Type check because the JSON serializer can return an array or dictionary
if([jsonObject isKindOfClass:[NSArray class]])
array = jsonObject;
}
// use your array variable here, it may be nil
You can use Apple's NSJSONSerialization class to do what your linked answer is using SBJsonWriter to do.
Example:
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:0 error:&error];
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Then write the jsonString to your keychain.
To reverse the process, retrieve the jsonString from your keychain and do this:
NSArray* myArray = [NSJSONSerialization JSONObjectWithData:jsonString options:0 error:&error];
I try to JSON encode NSDictionary into NSArray upload to PHP web-services.
This is how I JSON encode my NSArray:
NSError * error;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:my_array_1 options:0 error:&error];
NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
Then I insert this JSON String into another array by:
NSString * jsonRequest = [NSString stringWithFormat: #"{\"request\":\"syncBookmark\",\"bookmark_array\":\"%#\",\"user_id\":\"%#\"}", jsonString, user_id, nil];
NSData * requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
Before I request the webservices, the JSON encoded string is like:
{"request":"syncBookmark","bookmark_array":"[{\"PostId\":\"4\",\"last_edit_date\":\"2014-03-03 01:05:37\",\"BookmarkId\":\"1\",\"last_sync_date\":\"\",\"is_active\":\"1\"}]","user_id":"7"}
I can echo the 'user_id' in PHP which is '7' correctly, but when I check the array - is_array($bookmark_array) in PHP it just return FALSE. Am I doing the way wrongly to put arrays in array?
This is because you enter JSON string as string and it is obviously escaped by the stringWithFormat method.
You should create a NSDictionary and serialize that together. So instead of serializing my_array_1 variable, you will serialize the dictionary.
Create the dictionary:
NSDictionary* dictionary = #{ #"request" : #"syncBookmark", #"bookmark_array" : my_array_1 };
Then serialize the dictionary:
NSError * error;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
This is the correct way to serialize a JSON object, not inserting serialized objects into another string. Although I see why you wanted to do that.
Don't bother doing the stringByReplacingOccurrencesOfString stuff. Let NSJSONSerialization do all of the necessary quoting for you.
So, if you really need that jsonString as a string that your PHP will recursively un-encode, just JSON encode the array, and then put that in another dictionary and then encode that again:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:my_array_1 options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *dictionary = #{#"request" : #"syncBookmark",
#"bookmark_array" : jsonString,
#"user_id" : user_id};
NSData *finalData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
That should create a payload much like you said you wanted in your question.
Having said that, a more logical approach would be:
NSError *error;
NSDictionary *dictionary = #{#"request" : #"syncBookmark",
#"bookmark_array" : my_array_1,
#"user_id" : user_id};
NSData *finalData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
If that's what generated your payload, then your PHP wouldn't have to do multiple calls to json_decode.
How to convert NSDictionary to NSString which contains JSON of NSDictionary ?
I have tried like but without success
//parameters is NSDictionary
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
options:0
error:&error];
if jsonData is NSDictionary
NSString *str=[NSString stringWithFormat:#"json data is %#", jsonData];
OR if jsonData is NSData
NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];
If you just want to inspect it, you can create a NSString:
NSString *string = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
But if you're writing it to a file or sending it to a server, you can just use your NSData. The above construct is useful for examining the value for debugging purposes.