I did it easily in Android but Im little lost how to do it in iOs.
My json is like:
[{"name":"qwe","whatever1":"asd","whatever2":"zxc"},
{"name":"fgh","whatever1":"asd","whatever2":"zxc"}]
If I do:
NSData *jsonData = [data dataUsingEncoding:NSUTF32BigEndianStringEncoding];
rows = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];
in rows can I access with?
NSString *name = [[rows objectAtIndex:0] [objectForKey:#s"name"]];
Or how I do that? thanks.
FINALLY NSString *name = [[rows objectAtIndex:0] objectForKey:#"name"]]; WORKS! :D
I think that will work, however you want:
NSString *name = [[rows objectAtIndex:0] objectForKey:#"name"];
(dropped extraneous square brackets and use #"name" as string literal).
However, is the input JSON really in UTF-32 format?
NSMutableArray *row= [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];
for (int i=0; i<[row count]; i++) {
NSMutableDictionary *dict=[row objectAtIndex:i];;
NSString * name=[dict valueForKey:#"name"];
NSLog(#"%#",name);
}
}
Assuming the NSJSONSerialization operation was successful, simply:
NSString *name = rows[0][#"name"];
NSUTF32BigEndianStringEncoding is suspicious, json data is more usually NSUTF8StringEncoding.
Related
I've been following a tutorial on youtube on how to load external data from a database on to a iOS application. I understand the concept and how it works etc. But I'm having an error even though I'm following the video and from looking online it seems to be a common error.
- (void) retrieveData
{
NSURL * url = [NSURL URLWithString: getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
//set up our cities array
citiesArray = [[NSMutableArray alloc] init];
// loop through our json array
for (int i =0; i< jsonArray.count; i++)
{
//create our city object
NSString * cID = [[jsonArray objectAtIndex:i] objectForKey:#"id"];
NSString * cName = [[jsonArray objectAtIndex:i] objectForKey:#"cityName"];
NSString * cState = [[jsonArray objectAtIndex:i] objectForKey:#"cityState"];
NSString * cPopulation = [[jsonArray objectAtIndex:i] objectForKey:#"cityPopulation"];
NSString * cCountry = [[jsonArray objectAtIndex:i] objectForKey:#"country"];
// Add the city object to our cities array
[citiesArray addObject:[[City alloc]initWithCityName:cName andCityState:cState andCityCountry:cCountry andCityPopulation:cPopulation andCityID:cID]];
}
//reload our table view
[self.tableView reloadData];
}
The code fails on the third line.
jsonArray = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
Why is that? Any suggestions? The jsonArray has already been created, so I can't understand why it won't work.
I checked your code & i don't think there is any error.
I hope you've declared NSMutableArray *jsonArray in header (.h) file.
If you getting error in thos line :
jsonArray = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
than replace kNilOptions with simply nil.
If you getting crash than it might be because nil data. In that case you have to put condition before you store data in array something like this :
if (data != nil)
{
jsonArray = [NSJSONSerialization JSONObjectWithData: data options:nil error:nil];}
Hope it'll help.
How can i parse below data:
u = "{\"userid\":\"Living123\"}";
I need to get Living123 as a string.
You can use this library to convert NSString to NSDictionary.
Import "SBJson.h" in your .m file and use following code
NSDictionary *dictionary = [u JSONValue];
NSString *userId = [dictionary valueForKey:#"userid"];
Edit
Alternatively you can use NSJSONSerialization to convert NSString to NSDictionary
NSError *e = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:u options:NSJSONReadingMutableContainers error:&e];
How to store this data in NSArray?
I'm getting JSON response in this format like this:
{
1 = USA;
4 = India;
}
I need to convert this into NSArray
If your JSON is a string, get an NSData object first:
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
Then turn it into an NSDictionary using the NSJSONSerialization class:
NSError* error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
Since you said you want an NSArray, do the following:
NSArray *jsonArray = [jsonDict allValues];
Note that the order of the entries in the array is undefined, according to Apple's documentation. So if you need a particular order, you'll have to figure out a better approach.
This is JSON dictionary, First you convert this to NSDictionary from JSON.
NSDictionary *dictionary = //parse json using NSJSONSerilization
NSArray *array = [dictionary allValues];
allValues will return an array with all objects.
Try this:
NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dictionary =[jsonString JSONValue];
NSArray *array = [dictionary allValues];
NSLog(#"Array values = %#",array);
#"{\"details\": \"A friend request is already pending.\"}".. This is a json string coming from the server. i want to retrieve the object in a one string i.e is A friend request is already pending string is to be in one string. please tell me how to retrieve this.
i tried like this
NSString *str = #"{\"details\": \"A friend request is already pending.\"}";
NSLog(#"%#",str);
NSString *resultStr1 = [str stringByReplacingOccurrencesOfString:#"{" withString:#""];
NSString *resultStr2 = [resultStr1 stringByReplacingOccurrencesOfString:#"}" withString:#""];
//NSLog(#"%#",resultStr2);
//NSString *encodestr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(#"%#",encodestr);
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];
NSLog(#"%#",data);
NSError *error =Nil;
NSArray *JSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",JSON);
NSLog(#"%#",[JSON valueForKey:#"details"]);
Try this. COnvert your string into a dictionary and access it by keys. This will be helpful when you come across large group of data.
NSString *str = #"{\"details\": \"A friend request is already pending.\"}";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(#"%#", [dic objectForKey:#"details"]);
Try this may be it will help you
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: str dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &e];
NSLog(#"%#",[JSON valueForKey:#"details"]);
I am trying to parse a json here but keep on getting errors for some reason. my code is
NSString *string = [NSString stringWithFormat:#"http://api.wunderground.com/api/3c158b3b3cd6ce90/hourly/q/11758.json"];//, query];
NSString *string2 = [string stringByReplacingOccurrencesOfString:#" " withString:#"_"];
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString:string]];
NSError* error;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray* latestLoans = [res objectForKey:#"forecast"]; //2
NSArray *tmp = [latestLoans valueForKey:#"temp"];
NSLog(#"temp: %#", tmp);
and the error is
temp: (null)
Entity: line 2: parser error : Start tag expected, '<' not found
{
^
I don't get this because the URL is real and gets a response, and I need help with this
What you are looking for, "forecast", does not exist in the response. Perhaps what you are looking for is "hourly_forecast".
NSArray* latestLoans = [res objectForKey:#"hourly_forecast"]; //2
NSArray *tmp = [latestLoans valueForKey:#"temp"];