I am working on API integration, From API I am getting the response in dictionary format, While I serialise that data, it is sorting automatically
Here is my code
id resposeJSon = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
While I try to print the same in NSString it works as the response is received
NSString *jsonString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
I am expecting the serialised variable resposeJSon should be in the same format what I get from variable jsonString
Thanks in advance
I'm a new in iOS.
I always use from JSON string to extract value data which I want. But, I faced that make a JSON string and send to web server.
I have to make a JSON string this -> "errorCode":"1491"
and my code is below.
NSString *jsonRt = #"{ \"errorCode\":\"1491\"}";
NSLog(#"[D] jsonRt : %#", jsonRt);
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[jsonRt dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
NSLog(#"jsonObject=%#", jsonObject);
NSError *error;
NSData *jsdata = [NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:&error];
NSLog(#"jsdata=%#", jsdata);
return [NSString stringWithFormat:#"%#",jsdata];
If build and run this code, my app is stop. I can't debug from breakpoint because suddenly jammed step over button. Where I fix this code?
I forgot the function return type. This is NSString*.
i want to take 450
from the following:
NSString {status:0,val:450}
using objective c:
{status:0,val:450}
Please suggest me answer
I cannot completely understand your question . I think you have you have an JSON data like:
{
status:0;
val:450;
}
If you want this data in your app. You want to do
NSURL *blogURL = [NSURL URLWithString:#"https://your url here"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *data = [dataDictionary objectForKey:#"status"];
NSDictionary *item = [data objectForKey:#"val"];
Your JSON Data get in Dictionary format. You can access the Data using the Key. In here your keys are status and val.
I hope this links are help for you.
fetch parse json ios programming tutorial
and this Link:
json parsing in ios
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]);
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>\\\"}}\"}";