how to save json value in sqlite database in iphone - iphone

hi friend this is my JSON file apitest[18318:207] { ErrorCode = 900; ErrorMessage = ""; Result = ( { DeviceInfoId = 12; Email = "rocky#itg.com"; FirstName = Rocky; LastName = Yadav; ProfileImage = "<null>"; ThumbnailImage = "<null>"; UserId = 153; } ); TokenID = 59nUniliob; isError = 0; }
Can you please explain me how to parse the above JSON file and save the values in sqlite database this

You can use the SBJson parser to get a dictionary/array out of a json (depending on your response). You can then get the individual values from the dictionary/array and save them into your database.
The git repo also has some examples on how to use the parser.

Related

Unable to Extract Data from the Dictionary<String, Any> in swift

I have a Dictionary<String, Any> Data in the following format. I need to extract the values from it.I am unable to extract the values, initially it looked to me as JSON but it's of type Dictionary<String, Any>. Can anyone help me out.
**{
message = {
"pn_apns" = {
aps = {
alert = {
body = "Andy - Picture added!";
roomId = 1611711512726;
title = "New chat on flag - New flag ";
};
sound = default;
};
};
senderId = "XXXX#XXXXX";
text = "Euclid%20Ave./Flag%20%26%20Chat/New%20flag%20/Original/1AF3956D-AEC4-485C-B8C2-3F97AEE05C58.png";
type = P;
uuid = "XXXXXXX-70CB-4301-DDDD-FFFFFFFF";
};
timetoken = 16117629931914202;
uuid = "XXXX#XXXXX";
}**
This isn't a JSON.
Look at https://www.json.org/json-en.html site.
To check the string, use https://jsonlint.com

parsing twitter search json data using NSJSONSerialization

I am parsing twiter search api json data with NSJSONSerialization.Requirement is to search tweets by hashtag.In Twitter api console tool I am correctly getting data about 15 tweets.
written code is
if let results: NSDictionary = NSJSONSerialization .JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments , error: errorPointer) as? NSDictionary {
}
I am getting results value as
{
"search_metadata" = {
"completed_in" = "0.05";
count = 15;
"max_id" = 680240431771156480;
"max_id_str" = 680240431771156480;
"next_results" = "?max_id=680240407322689535&q=%23ChristmasEve&include_entities=1";
query = "%23ChristmasEve";
"refresh_url" = "?since_id=680240431771156480&q=%23ChristmasEve&include_entities=1";
"since_id" = 0;
"since_id_str" = 0;
};
statuses = (
{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Fri Dec 25 04:15:31 +0000 2015";
entities = {
hashtags = (
{
indices = (
0,
13
);
text = ChristmasEve;
},
{
which is incomplete.
I even tried using SwiftyJSon library but I am getting similar results.
Is there any way to get statuses/Tweet info value without using any external library?
Given the fact that you mentioned you are getting multiple tweets (15), the JSON data you're getting back from the API possibly is an array, not a dictionary. It's a good practice to handle both cases when you make network calls:
do {
let object = try NSJSONSerialization.JSONObjectWithData(data, options: [])
if let dictionary = object as? [NSObject: AnyObject] {
// Handle dictionary
} else if let array = object as? [[NSObject: AnyObject]] {
// Handle array
}
} catch {
}

How to create dictionary from JSON

I receive JSON objects that looks like the one below. How can I convert it into a format that is easily handled by Swift 2.1 ? I will receive several of these and have to put them into an array and sort by createdAt.
Optional({
comment = "<null>";
completedAt = "<null>";
createdAt = "2015-11-02 15:01:04 +0000";
paid = 1;
paidAt = "2015-11-02 15:01:04 +0000";
startedAt = "<null>";
state = request;
type = doctor;
user = KTsCySacEAiz3eDnf;
userdata = {
birthdate = "<null>";
gender = "<null>";
};
})
As in the title you say you expect a Dictionary, you can get one by simply serializate the json and cast it to a Dictionary.
do{
let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
}catch{
print("Something went wrong")
}
Where data is you json content as NSData. If you get a String you can easily convert it
let data = text.dataUsingEncoding(NSUTF8StringEncoding)

UITableView+YouTubeAPI parsing JSON response for statistics

I am getting the JSON response from youtube API v3 for https://www.googleapis.com/youtube/v3/channels?part=statistics&id
and receiving JSON response as
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/2zirKmd0WgUqY0RzlyD4hlACeWM\"";
items = (
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/u4TmQ5XfIQQg6y6u4Od2yULCVlc\"";
id = "HCcrj0EHvn_Y8";
kind = "youtube#channel";
statistics = {
commentCount = 0;
subscriberCount = 21694;
videoCount = 124582;
viewCount = 0;
};
},
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/2p4_mjrZLfhO6bDvH-RgAykNQr8\"";
id = UCX2v47KsDKqajrEYFV7GbBg;
kind = "youtube#channel";
statistics = {
commentCount = 6460;
subscriberCount = 494656;
videoCount = 33;
viewCount = 713607227;
};
}
}
when I am trying to display the
cell.viewsChannel.text=[[_statistics valueForKeyPath:#"statistics.videoCount"] objectAtIndex:indexPath.row];
the value doesn't display on table view.
I tried to format the output as unsigned long ,int but the correct values don't show.
Please help
Thanks in advance
I think you need to add item to valueForKeyPath
cell.viewsChannel.text=[[_statistics valueForKeyPath:#"item.statistics.videoCount"] objectAtIndex:indexPath.row];

How to get Facebook Photo Albums for iOS?

I'm making the following simple request to Facebook. My goal is to grab the names of all a user's photo albums:
[[AppDelegate sharedDelegate].facebook requestWithGraphPath:#"me/albums" andDelegate:[AppDelegate sharedDelegate]];
Then, I get a JSON response from the server like such:
data = (
{
"can_upload" = 1;
count = 4;
"cover_photo" = 321491374598618;
"created_time" = "2012-06-04T21:46:23+0000";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 321491371265285;
link = "http://www.facebook.com/album.php?fbid=321491371265285&id=100002132746588&aid=73680";
name = "Photos";
privacy = custom;
type = normal;
"updated_time" = "2012-06-04T22:08:39+0000";
},
{
"can_upload" = 0;
count = 1;
"cover_photo" = 318401854907570;
"created_time" = "2012-05-31T00:00:35+0000";
description = "Which Friend Stalks You the Most?";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 318401848240904;
link = "http://www.facebook.com/album.php?fbid=318401848240904&id=100002132746588&aid=73163";
name = "Which Friend Stalks You the Most?";
privacy = friends;
type = normal;
"updated_time" = "2012-05-31T00:00:36+0000";
},
And so on. Problem is, when I try to parse the data with:
NSLog(#"%#", [result objectForKey:#"name"]);
I get null. I assume this is happening because the NSDictionary that the data is returned in does not know which name entry to concentrate on. How do I parse the data so that I get the names of all the albums?
"result" is actually an array of dictionaries, so loop through the array and then grab name from each element in the array.
for (NSDictionary *anAlbum in [result objectForKey:#"data"]) {
NSLog(#"%#", [anAlbum objectForKey:#"name"]);
}