I need json string without double quotes in keys in ios - ios

I upload the json string to the server like
NSDictionary *values = [[NSDictionary alloc]initWithObjectsAndKeys:#"name", #"objective", nil];
NSArray *array = [[NSArray alloc]initWithObjects:values,nil];
NSDictionary *result = [[NSDictionary alloc]initWithObjectsAndKeys:array, #"objectives", nil];
I use the following code to convert the dictionary to JSON:
id obj = result;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:kNilOptions error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
I get correct json like
{ "objectives":[
{"objective":"name"}
]
}
This is correct json but I need json string like
{ objectives:[
{"objective":"name"}]
}
Is it possible to create without double quotes in key in iOS?
Only this json string we get the response otherwise I get error.

if it is only the first key that you want to remove quotes from, you can use the following quick workaround
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\"objectives\"" withString:#"objectives"]];

Related

NSDictionary structure not getting done properly

i want to make a structure like following using NSDictionary
{
"request":
{
"email":"manger#abc.com",
"password":"manager"
}
}
I'm using the following code in objective C
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
#"manger#hall.com",#"email",
#"manager",#"password",
nil];
NSDictionary *req = [[NSDictionary alloc] initWithObjectsAndKeys:
paramsDic,#"request",
nil];
but it returns the following response
{
request =
{
email = "manger#abc.com";
password = manager;
};
}
it shows semicolons instead of commas , one extra semicolon and keys are not in inverted commas
Convert req to JSON object
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:req options:NSJSONWritingPrettyPrinted error:nil];
NSLog(#"%#", jsonData);

Deserialise JSON String in Objective C

I received NSData object data from REST API. That contains JSON data which I want to parse.
{
JsonResult = "[{
\"IsAuth\":\"true\",
\"User\":\"
[
{
\\\"userid\\\":\\\"josephH\\\",
\\\"firstname\\\":\\\"joseph\\\",
\\\"lastname\\\":\\\"Henry\\\",
}
]\"}]"
}
This statement gave me the result as a String like below which I am not able to parse as JSON.
myData = [data valueForKey:#"JsonResult"];
"[{
\"IsAuth\":\"true\",
\"User\":\"
[
{
\\\"userid\\\":\\\"josephH\\\",
\\\"firstname\\\":\\\"joseph\\\",
\\\"lastname\\\":\\\"Henry\\\",
}
]\"}]"
When I try to pass this mydata to JSONSerialization the code crashes.
How do I cast the above string to NSDictionary so that I can parse them and use the values of IsAuth and User.?
Code:
[LDService authenticateUser:Uname.text passwordString:Password.text completeBlock:^(NSData * data){
NSError *error;
NSData *jsonData;
NSString *jsonString = nil;
NSMutableDictionary *jsonDict;
if([NSJSONSerialization isValidJSONObject:data])
{
jsonData = [NSJSONSerialization dataWithJSONObject:data
options:kNilOptions
error:&error];
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSString *formattedString = [jsonString stringByReplacingOccurrencesOfString:#"\\\"" withString:#"'"];
NSLog(#"Formatted string %#",formattedString);
[jsonDict setObject:formattedString forKey:#"JsonResult"];
NSLog(#"Parsed json %#",jsonDict);
}];
Pass your data as data
NSError *error;
NSString *jsonString = nil;
if([NSJSONSerialization isValidJSONObject:data])
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data
options:kNilOptions
error:&error];
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
then replace occurance of #"\\\" with #"'"
NSString *formattedString = [jsonString stringByReplacingOccurrencesOfString:#"\\\"" withString:#"'"];
then use this formattedString.
I have investigates your json file from Json formatter & Validator, there are lots of error in your json file, so first check your file from this validator and this formatter gives you error with description. Re-build your json file, if you still getting any problem then ask.

Getting element from NSString

my app get data from a php web service and return a NSString
["first name","last name","adress","test#test","000-000-0000","password","code","0"]
How can i get the second element ?
This is a JSON formatted string which you are getting from your web service.
You must be getting bytes from server. Just replace your variable which have bytes stored with my variable "response data".
Code:
NSError* error;
NSArray* myResultArray = [NSJSONSerialization
JSONObjectWithData:responseData options:kNilOptions error:&error];
You will get an array in variable "myResultArray" and you can get all value by index.
Code:
NSString* first name = [myResultArray objectAtIndex:0];
What you have given here is an array and not a string. May be you could provide more details like the exact response and the code that you are trying here.
To Convert a JSON string to NSDictionary all you need to do is:
NSError *jsonError;
NSData *objectData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:nil
error:&jsonError];
And to NSArray :
NSArray *array = [NSJSONSerialization JSONObjectWithData:objectData options:nil error:&jsonError];
NSString *secondElement = array[1];
That web service doesn't return an NSString. It runs data in some format defined by the service, and you convert it to an NSString. Find out what the format actually is, then convert it appropriately, for example using NSJSONSerialization.
The example string your showed here seems wired. This looks more like a array than string. If this is a NSArray then you can do it like this:
NSArray *data = #[#"first name",#"last name", #"adress", #"test#test", #"000-000-0000", #"password", #"code", #"0"];
NSLog (#"Second component = %#", data[1]);
However, if you anticipate this as NSString then this is how you would handle this:
NSString *test = #"[\"first name\",\"last name\",\"adress\",\"test#test\",\"000-000-0000\",\"password\",\"code\",\"0\"]";
NSLog (#"Second component = %#", [test componentsSeparatedByString:#","][1]);

SNSPublishRequest how to send the aps array and not see the "default" message for APNS

I am trying to publish to SNS using the SNSPublishRequest but I can't figure out how to format the JSON to actually use the parameters set for the aps dictionary. If I put in something for the "default" it will send that message. But if I add the APNS dictionary as well, it seems to not show anything. This is how I am formatting the JSON request - am I missing something?
NSDictionary *parameters = #{#"default" : #"",
#"APNS" : #{#"aps": #{#"alert": #"hello"}}};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
options:0
error:&error];
NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(#"JSON OUTPUT: %#",JSONString);
SNSPublishRequest *pr = [[SNSPublishRequest alloc] initWithTopicArn:#"someTopic" andMessage:JSONString];
pr.messageStructure = #"json";
I think SNS's APNs dictionary expects an JSON encoded string format itself. We have to escape all the " and add the \s
NSString* JSONString = #"{\"default\": \"<enter your message here>\",\"APNS_SANDBOX\":\"{\\\"aps\\\":{\\\"alert\\\":\\\"<HELLO>\\\"}}\"}";

SBJSON issues with key value NSDictionary

I have got JSON string as below:
{"status": "ok", "secret": "d2d388eb3a24aaacb365597f69a2bf97", "client": "100006", "expires": "1345721042"}
For parse JSON object I'm using SBJSON library.
This is my code that I have used for retrieve data from JSON object:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSError *error = nil;
NSArray *jsonObjects = [jsonParser objectWithString:[request responseString] error:&error];
but when I try to out into NSLog my dictionary I have not a pair key value object, but have just key for example: status, secret, client and expires.
How can I get NSDictionary with key value object using SBJSON library
{
status = "ok";
secret = "d2d388eb3a24aaacb365597f69a2bf97";
}
It's because you are setting the result of your parse to a NSArray object. You should store the results of the JSONParser as a NSDictionary object to store it as key value pairs
NSDictionary *jsonObjects = [jsonParser objectWithString:[request responseString] error:&error];
Then you can access them like so:
NSString *status = [jsonObjects objectForKey:#"status"];
NSString *secret = [jsonObjects objectForKey:#"secret"];
// and so on

Resources