How to parse json data in nsstring?
you need to do like
dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *temp = (NSString*)[dictionary valueForKey:@"token"];
NSLog(@"temp %@", temp);
NSDictionary *data = [dictionary valueForKey:@"data"];
NSString *tnamemp = (NSString*)[data valueForKey:@"name"];
NSString *role = (NSString*)[data valueForKey:@"role"];
iOS JSON NSString Parse
The way to do it with iOS 5 is to use the NSJSONSerialization
class. You will want to first convert your string to an NSData object, and call the class method JSONObjectWithData
NSData *jsonData = [myJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
Note that JSONObjectWithData
will return either an NSDictionary or an NSArray, depending whether your JSON string represents an a dictionary or an array.
How to parse jsonstring in iOS using objective c?
if json is in respose data then
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:request.responseData options:kNilOptions error:&err];
if json is in string then
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding]; options:kNilOptions error:&err];
NSString* loginMethod= [jsonDic objectForKey:@"method"];
NSString* usename= [[jsonDic objectForKey:@"data"]objectForKey:@"username"];
NSString* password= [[jsonDic objectForKey:@"data"]objectForKey:@"password"];
How to parse this kind of json String in ios
It is simple.
According to your response it is confirmed that your response is a dictionary.and in your recipeDictionary there a array object associated with contacts key.
So take this value in array.Like this
NSArray *yourContactsArray = [recipeDictionary valueForKey:@"contacts"];
Now this array contains the multiple objects which are in form of dictionary.I am printing all the values in name key to demonstrate you fetching.
for (NSDictionary *dict in yourContactsArray)
{
NSString *name = [dict valueForKey:@"name"];
NSLog(@"%@",name);
}
Converting an NSString of JSON Data to NSArray
// Your JSON data:
NSString *colorArray = @"[{ \"color\":\"Red\" },{ \"color\":\"Blue\" },{ \"color\":\"Yellow\"}]";
NSLog(@"colorArray=%@", colorArray);
// Convert to JSON object:
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[colorArray dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
NSLog(@"jsonObject=%@", jsonObject);
// Extract "color" values:
NSArray *tableData = [jsonObject valueForKey:@"color"];
NSLog(@"tableData=%@", tableData);
Output:
colorArray=[{ "color":"Red" },{ "color":"Blue" },{ "color":"Yellow"}]
jsonObject=(
{
color = Red;
},
{
color = Blue;
},
{
color = Yellow;
}
)
tableData=(
Red,
Blue,
Yellow
)
The last step uses the special feature of -[NSArray valueForKey:]
that returns an array containing the results of invoking valueForKey:
using the key on each of the array's objects.
How to parse the data into Json Format in ios?
If you got you response in json string then try like below,
NSError *jsonError;
NSData *objectData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
here responseString
is final output json string that you have posted in question.
Then fetch data from json dictionary
.
If this scenario will not work then take a look at NSXMLParser, you can refer Appcoda's tutorial.
How to Parse Json with NSJSONSerialization from NSString
NSJSONSerialization uses NSData not NSString.
Replace your line...
NSString *text = [[NSString alloc] initWithData...
With something like this...
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
The dictionary object will now be a representation of the JSON.
This depends on the JSON being correct though :-)
Also, this has been asked hundreds of times before.
How to convert escaped JSON-string to NSString?
By default, Foundation will only parse JSON objects or arrays, but it can parse strings, numbers and booleans if you tell it to accept JSON fragments:
NSData *data = [@"\"Some text.\\nSome text.\\n\\t\\\"Some text in quotes.\\\"\""
dataUsingEncoding:NSUTF8StringEncoding];
id result = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:NULL];
NSLog(@"[result class] = %@", [result class]);
NSLog(@"result = %@", result);
Yields:
[result class] = __NSCFString
result = Some text.
Some text.
"Some text in quotes."
This is actually one of the cases where passing error
really helps. I had tried without passing NSJSONReadingAllowFragments
and got a very clear error message:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.)
parse JSON string to NSDictionary into array of objects Objective C
Try this code to get value
NSMutableArray *res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",res);
// get item id
NSLog(@"item-id :%@",[[res objectAtIndex:0] objectForKey:@"item_id"]);
NSLog(@"datetime %@",[[res objectAtIndex:0] objectForKey:@"datetime"]);
NSLog(@"main_url %@",[[res objectAtIndex:0] objectForKey:@"main_url"]);
or get all value from response
for (int i=0; i< res.count; i++){
NSLog(@"item-id :%@",[[res objectAtIndex:i] objectForKey:@"item_id"]);
NSLog(@"datetime %@",[[res objectAtIndex:i] objectForKey:@"datetime"]);
NSLog(@"main_url %@",[[res objectAtIndex:i] objectForKey:@"main_url"]);
}
Related Topics
Determine Mime Type from Nsdata
Rendering PDF in Uiwebview iOS 8, Causes a Black Border Around PDF
iOS 9.3:An Ssl Error Has Occurred and a Secure Connection to the Server Cannot Be Made
Making Video from Uiimage Array with Different Transition Animations
Uiviewcontroller Prevent View from Unloading
Text Color Based on Background Image
Swift: Testing Optionals for Nil
Chrome iOS - Is It Just a Uiwebview
How to Pop Specific View Controller in Swift
App Crashes on Enabling Camera Access from Settings iOS 8
Service Workers and iOS/Safari
How to Select Range of Values When Using Arc4Random()
How to Get Visible Viewcontroller from App Delegate When Using Storyboard
How to Vertically Center Uitextfield Text
iOS Keychain Services: Only Specific Values Allowed for Ksecattrgeneric Key
Multiline Uilabel with Adjustsfontsizetofitwidth