I'm trying to parse this JSON that looks to me like well written, but NSJSONSerialization doesn't think the same AFAIK since it's returning an NSArray.
This is my code:
NSData* gamesData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://s42sport.com/polarice/json/games.json"]
];
NSDictionary* json = nil;
if (gamesData) {
json = [NSJSONSerialization
JSONObjectWithData:gamesData
options:kNilOptions
error:nil];
NSLog(#"%d",json.count);
}
The questions are,
What's wrong with the JSON? Why NSSerialization doesn't return me the NSDictionary?
Edit: Yes, I just learned about the [...] vs {...}. Thank You.
Parse your json by this way.
NSURL * url=[NSURL URLWithString:#"http://s42sport.com/polarice/json/games.json"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary * json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(#"%#",json);
NSArray * array1=[json valueForKey:#"c"];
NSLog(#"%#",array1);
Try this code. this will surely work for you.
NSDictionnary should be used for Object whereas NSArray is use for JSON array
NSArray* json = nil;
if (gamesData) {
json = [NSJSONSerialization
JSONObjectWithData:gamesData
options:kNilOptions
error:nil];
NSLog(#"%d",json.count);
}
The JSON file you listed is an array (it starts and ends with a square bracket) so Objective-C reflects that with an NSArray root object.
Related
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am very new to iOS application development and I am getting the below response from the server:
"[{\"EmployeeID\":\"000001\",\"EmplyeeName\":\"ABCD EFGHI\"},
{\"EmployeeID\":\"000002\",\"EmplyeeName\":\"ADGHT ASASASAS\"}]"
Please anybody help me out on how to use employee ID and employee name in my application.
Your JSON data looks like "nested JSON", which means that you have to deserialize it twice.
The first deserialization extracts a string from your JSON data:
NSData *response = ...; // your data
NSError *error;
NSString *innerJson = [NSJSONSerialization JSONObjectWithData:response
options:NSJSONReadingAllowFragments error:&error];
Now innerJson is the string
[{"EmployeeID":"000001","EmplyeeName":"ABCD EFGHI"},{"EmployeeID":"000002","EmplyeeName":"ADGHT ASASASAS"}]
which is again JSON data. The second deserialization extracts the array:
NSArray *entries = [NSJSONSerialization JSONObjectWithData:[innerJson dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
And now you can access it like
for (NSDictionary *entry in entries) {
NSString* employeeID = [entry objectForKey:#"EmployeeID"];
NSLog(#"%#", employeeID);
}
NSData *response = ...;
NSArray *entries = [NSJSONSerialization JSONObjectWithData:response
options:0
error:nil];
for(NSDictionary* entry in entries) {
NSString* employeeID = [entry objectForKey:#"EmployeeID"];
}
Look JSON Parser your response is JSON so you need to get that data from json.
NSURL * url=[NSURL URLWithString:#"<YourURL>"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSLog(#"%#",json);
NSMutableArray * EmployeeID=[[NSMutableArray alloc]init];
NSMutableArray * EmployeeName=[[NSMutableArray alloc]init];
NSArray * responseArr = json[#"geonames"];
for(NSDictionary * dict in responseArr)
{
[EmployeeID addObject:[dict valueForKey:#"EmployeeID"]];
[EmployeeName addObject:[dict valueForKey:#"EmplyeeName"]];
}
now you get the EmployeeID,EmployeeName array now you can used anywhere where you want.
Take a look at:
NSJSONSerialization
Use this:
NSData* yourData = [[NSData alloc] initWithContentsOfURL: yourURL];
NSarray* yourJSON = [NSJSONSerialization JSONObjectWithData:yourData options:kNilOptions error:nil];
Hope this help you :)
Try to use this:
NSDictionary *myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
I have a question how to conver NSData to NSMultableArray
I recive the data from JSON and I need to set that data into the Table View.
How to do that?
Above is the example of the JSON:
{"d":"[{\"codUf\":28,\"descricao\":\"MG\"},{\"codUf\":29,\"descricao\":\"PR\"},{\"codUf\":19,\"descricao\":\"RJ\"},{\"codUf\":25,\"descricao\":\"SP\"}]"}
Thanks!
Use SBJson https://github.com/stig/json-framework/
or else you can do
NSError *error = nil;
[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]
Try
NSData *responseData = ...
NSError *error = nil;
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:&error];
If you are using IOS 5 + you can NSJSONSerialization and if you are supporting older version as well you can use SBJSON.
This question might help you.
Decode JSON to NSArray or NSDictionary
I'm new to JSON and don't understand why this is failing. The JSON is valid according to on-line validation tools, but NSJSONSerilization says this the string is invalid. Why is it invalid?
NSString* JSON = #"{\"Questionnaire\":{\"questionnaireid\":1,\"modifiedDate\":\"2012-12-28 15:27:00\"}}";
if (![NSJSONSerialization isValidJSONObject:JSON]) {
return nil;
}
NSError *jsonParsingError = nil;
NSDictionary* data = [NSJSONSerialization dataWithJSONObject:JSON options:NSJSONReadingMutableContainers error:&jsonParsingError];
why you did serialization when you already created JSON yourself ?
What you should do:
NSData *jsonPayload = [JSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:jsonPayload
options:kNilOptions error:&error];
Because a JSON Object must be of type NSArray or a NSDictionary while you are passing a NSString.
From the docs:
An object that may be converted to JSON must have the following
properties:
The top level object is an NSArray or NSDictionary.
All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
All dictionary keys are instances of NSString.
Numbers are not NaN or infinity.
UPDATE
You probably want to do this:
NSData *jsonData = [JSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
quite new to iOS development and objective-c at the same time. I have the following method:
-(NSMutableArray *)fetchDatabaseJSON{
NSURL *url = [NSURL URLWithString:#"http://www.ios.com/ios/responseScript.php"];
NSError *error = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];
//jsonArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
//NSLog(#"Array: %#",[jsonArray objectAtIndex:0]);
jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(#"Dictionary: %#", jsonDictionary);
return jsonArray;
}
Now the NSLog shows this:
2013-02-03 19:15:37.081 TestConnection[24510:c07] Dictionary: (
Bannana,
Apple,
SomeCheese )
From what I understand that whatever is inside the dictionary doesn't have key-value. How can this be? and how can I fix it? I want to be able to have keys to ease operations on dictionary.
Regards,
JSONObjectWithData may return an NSArray or NSDictionary, depending on the JSON data you give it. If your JSON string is an array, you will have an NSArray. If your JSON data is a dictionary, you will get an NSDictionary.
Convert your JSON data (your data variable) to string and print it out with NSLog. To convert NSData to NSString, use something like:
NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
If you print it out and see a JSON array, you simply don't have a dictionary there.. If you can alter the server-code that generates the JSON, you may be able to change that.
One more thing I noticed, you assume that the returning container is mutable. If I'm not mistaken, you need to use an option like NSJSONReadingMutableContainers in the options parameter of JSONObjectWithData to get that.
One last tip, if you want to check in code if you have an NSArray (or NSDictionary), use something like:
if ([obj isKindOfClass:[NSArray class]]) {...}