parse json feeds using jsonkit iOS [closed] - ios

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm trying to use the JSONKIt found here https://github.com/johnezang/JSONKit to parse through a JSON Feed and put into objective-c objects. I'm new at iOS and don't really know where to start. Are there any good tutorials for using this library?

After googling, I didn't find any tutorials but using JSONKit should be self explanatory.
After downloading your JSON feed using NSURLConnection or ASIHTTPRequest simply create a dictionary of all the objects in the JSON feed like so:
//jsonString is your downloaded string JSON Feed
NSDictionary *deserializedData = [jsonString objectFromJSONString];
//Helpful snippet to log all the deserialized objects and their keys
NSLog(#"%#", [deserializedData description]);
After creating a dictionary you can simply do something like this:
NSString *string = [deserializedData objectForKey:#"someJSONKey"];
And that is the basics behind JSONKit.
JSONKit is much more powerful of course, you can find some of the other things you can do with it in JSONKit.h

I would becareful about making the assumption that objectFromJSONString is returns an NSDictionary, it can very well return an array, or nil, especially if the server returns some rarely used and thought of error.
A more appropriate action would be:
NSError *error;
id rawData = [jsonString objectFromJSONStringWithParseOptions:JKParseOptionNone error:&error];
if ( error != nil ) {
// evaluate the error and handle appropriately
}
if ( [rawData isKindOfClass:[NSDictionary class]] ) {
// process dictionary
}
else if ( [rawData isKindOfClass:[NSArray class]] ) {
// process array
}
else {
// someting else happened, 'rawData' is likely 'nil'
// handle appropriately
}
Without these checks, you could very well end up with a runtime error because the server returned someting unexpected.

Related

How to create a simple API in Firebase? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have never worked on the backend and never created an API, all I know is to handle the JSON response, This doesn't help much, but I do not understand "PUT - Writing Data".
However, I just want to know that how can I create a simple dictionary of multiple arrays or key-values as rest API, So that it can be fetched and i can show the data in table view etc.
According to Firebase Documentations, if you wanna create a node on Firebase realtime database, you can simply set a value to any key you want. The value here should be NSDictionary.
This is the code you can refer to:
// create a new node
[[[_ref child:#"users"] child:user.uid] setValue:#{#"username": username}];
// you can also use update to create a new node
NSString *key = [[_ref child:#"posts"] childByAutoId].key;
NSDictionary *post = #{#"uid": userID,
#"author": username,
#"title": title,
#"body": body};
NSDictionary *childUpdates = #{[#"/posts/" stringByAppendingString:key]: post,
[NSString stringWithFormat:#"/user-posts/%#/%#/", userID, key]: post};
[_ref updateChildValues:childUpdates];
Hope it helps.

Whats the best way to parse JSON format in iOS Swift? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a project that polls a URL that returns a JSON format values. Any recommendations of whats the best way to parse the result for my iOS app?
First of all there is no such thing as best way. If there was a best way, you will probably heard it or find it among top google hits.
You can make do-it-yourself with NSJSONSerialization. This what Apple provides, and it is merely the fastest and the hardest to use. It's not even that 'hard', it just get's complicated when JSON has manny sub-levels.
What I can recommend you is SwiftyJSON. It had minor(barely noticable in most apps) overhead but it's much more easier to use in Swift. A great example is found on raywenderlich site.
You can also simply parse
var data = NSData(contentsOfURL: NSURL(string: "http://api.androidhive.info/contacts/")!)
var parcedData : NSMutableDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil) as! NSMutableDictionary
print(parceData)
There are many other ways to do it.
You can use Alamofire with SwiftyJSON
Snippet With Alamofire and SwiftyJSON
Alamofire.request(.GET, url, parameters: parameters)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
println(req)
println(res)
}
else {
NSLog("Success: \(url)")
var json = JSON(json!)
}
}

Swift: Create table of results from SwiftyJson Results [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been playing around with Swift as I'm new to iOS Development and really enjoying getting to know the language. Recently I have started to look at installing third party plugins etc and I found one called SwiftyJson (https://github.com/SwiftyJSON/SwiftyJSON) I have managed to get it to work using this script:
request(.GET, url, parameters: parameters)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
println(req)
println(res)
}
else {
NSLog("Success: \(url)")
var json = JSON(json!)
println(json)
}
}
I would like to know two things: 1) Is it possible to get the list of results from the JSON provided into a UITableView like so:
Item 1
----------------------------
Item 2
----------------------------
Etc.. and 2) How would I do that?
Edit: Json
Sorry about image - I couldn't copy it because of a stupid chrome extension I have:
Yes it is possible, though you aren't really using the important features of SwiftyJSON in this code sample. The most straightforward way would be to create the tableview the same as you would for anything, likely using an array as your datasource. Once you've received your JSON you still need to parse it and create the objects for your array. Perhaps if you included a snippet of the JSON, it would be easier to help.

JSON file parsing gives object name rather contents [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I do have a JSON file I created here: http://www.jsoneditoronline.org/?id=d0b62425c78f98db2398ed558f92e5cf
Simply, I added this file to my project and trying to parse it. For some reason, NSLog line gives me "statement" as a result rather than the statement object. I was expecting it to give me the whole dictionary of statement object instead. I am clearly missing something here. I would appreciate any help.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"math" ofType:#"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"jsonObject is %#", jsonObject); //this gives me the whole JSON object correctly.
for (NSDictionary *dict in jsonObject[#"statements"]) {
NSLog(#"dict is %#", dict);
}
That is screwy JSON. Normally one would expect the value of "statements" to be an array of objects, not just a single object. When you iterate on a dictionary (JSON "object") you iterate through the keys, so you're getting what would be expected.

Combining arrays into a url string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So I'm building an array of url strings using extensions. I have two arrays of extensions and Im a little bit confused on how to combine them.
I am trying to produce an array of string objects with the following format:
http://mywebsite.com/images/places/extention1/extention1
http://mywebsite.com/images/places/extention2/extention2
and so on...
When I try to combine them the way I know how I end up with the full array stuck in the place holders
http://mywebsite.com/images/places/full array1/full array 2
but what I'm really trying to do is build an array of those URL strings from the other two arrays
i know this is super simple but I haven't found any documentation on combining them into a skeleton string.
This would do the trick:
NSMutableArray *urlArray = [[NSMutableArray alloc] init];
NSArray *extensions = #[#"extension1", #"extension2", #"extension3"];
NSString *urlString = #"http://mywebsite.com/images/places/";
for (NSString *extension in extensions) {
NSString *combined = [[urlString stringByAppendingPathComponent:extension] stringByAppendingPathComponent:extension];
[urlArray addObject:combined];
}
There are tons of ways to do this but it could be as simple as:
NSArray *array = [NSArray arrayWithObjects:#"http://mywebsite.com/images/places/extention1/extention1", #"http://mywebsite.com/images/places/extention1/extention2", nil];

Resources