JSON Parsing not giving Proper NSMutable Array : - iphone

I am working on one Webservice app .
They give me response like :
JSON RESPONSE:
{"1":"03:30 PM To 04:30 PM","2":"01:30 PM To 02:30 PM","3":"02:30 AM
To 03:30 PM","4":"04:30 PM To 05:30 PM","5":"05:30 PM To 06:30
PM","6":"06:30 PM To 07:30 PM"}
And I want to Show all this TIME intervel with Check box in my another View controller so User can select it .
How do I get this ...
Need help

Check out the documentation for NSArray componentsSeparatedByString:(NSString *). This will separate the Json reply into an array at the string you specify. I would probably use #",".

You can use SBJson classes to parse json data,
here is the like to get all classes
SBJson

You can simply parse the JSON by following Method
NSString* strJSONResponse = #"{\"1\":\"03:30 PM To 04:30 PM\",\"2\":\"01:30 PM To 02:30 PM\",\"3\":\"02:30 AM To 03:30 PM\",\"4\":\"04:30 PM To 05:30 PM\",\"5\":\"05:30 PM To 06:30 PM\",\"6\":\"06:30 PM To 07:30 PM\"}";
NSDictionary* dataDict = [strJSONResponse JSONValue];
NSLog(#"%#",dataDict);
Here is the output:
Printing description of dataDict:
{
1 = "03:30 PM To 04:30 PM";
2 = "01:30 PM To 02:30 PM";
3 = "02:30 AM To 03:30 PM";
4 = "04:30 PM To 05:30 PM";
5 = "05:30 PM To 06:30 PM";
6 = "06:30 PM To 07:30 PM";
}
With this dictionary you can simply make use as per your requirement.
Here you will need to include Required classes for SBJSON found here
I had to add "\" as an escape characters to use it as a string.
Hope it helps

Related

How can i ParseExact Wed Jun 27 08:50:00 2018 -0500

Im trying to use [datetime]::ParseExact
The string i need to convert to datetime is Wed Jun 27 08:50:00 2018 -0500
I couldnt figure out the correct format to convert it correctly.
Please help me out. Thanks
Try this as well:
$Date = 'Wed Jun 27 08:50:00 2018 -0500'
[datetime]::ParseExact($Date,"ddd MMM dd HH:mm:ss yyyy zzz",[CultureInfo]::InvariantCulture)
There are several options. To some extent it will depend on what you want to do with that time offset. Here is one way that ignores the offset:
$test = 'Wed Jun 27 08:50:00 2018 -0500'
$parts = $test.split(' ')
$date = get-date ('{0}{1}{2} {3}' -f $parts[2], $parts[1], $parts[4], $parts[3])

Change date format 08 / 03 / 2017 to 08 March 2017

I'm trying to change date format from 08/03/2017
08 day
03 Month
2017 year
I I'm using
date("d F Y", strtotime($date));
the problem is that I Get 03 August 2017 instead of 08 March 2017
PS : I can't use any other then
dd/mm/yyyy
Try this
$date = DateTime::createFromFormat('d/m/Y', "08/03/2017");
echo $date->format('d F Y');
You can check it to http://php.net/manual/en/datetime.createfromformat.php
After Some researchs I found a solution
str_replace(' / ', '-',$date)
/*
You got August because you provide wrong format to this function. Function accept m/d/y and you provide d/m/y. you can try with this format m/d/y
or
you can try this with DateTime object.
*/
$date = "08/03/2017";
$dateObject = DateTime::createFromFormat('d/m/Y', $date);
echo $dateObject->format('d M Y');

Automatically put working week

Right now i am working on a weekly basis gathering the data and put the week and month manually. For example: The working week for today this week is June 23 thru June 29. and the month is June 2014.
I want to gather the YTD data and based on the date put the Week and Month automatically
For example:
Referral Request Date Week Month
1/3/2014 0:00 December 30 thru January 05, 2014 January 2014
1/3/2014 11:10 December 30 thru January 05, 2014 January 2014
12/31/2013 0:00 December 30 thru January 05, 2014 December 2013
6/18/2014 0:00 June 16 thru June 22, 2014 June 2014
6/20/2014 9:51 June 16 thru June 22, 2014 June 2014
4/28/2014 16:34 April 28 thru May 04, 2014 April 2014
5/1/2014 15:22 April 28 thru May 04, 2014 May 2014
The working week will begin each monday and finished on Sunday.
It can be do automatically?? The file have thousand of lines...
Here you are:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use DateTime;
use DateTime::Format::Strptime;
my #datetimes = (
'1/3/2014 0:00',
'1/3/2014 11:10',
'12/31/2013 0:00',
'6/18/2014 0:00',
'6/20/2014 9:51',
'4/28/2014 16:34',
'5/1/2014 15:22',
);
for my $datetime_str (#datetimes) {
my $strp = 'DateTime::Format::Strptime'->new( pattern => '%m/%d/%Y %H:%M' );
my $dt = $strp->parse_datetime($datetime_str);
my $month_year_strp = 'DateTime::Format::Strptime'->new( pattern => '%B %Y' );
my $month_year = $month_year_strp->format_datetime($dt);
my $desired_dow = 1; # Monday
$dt->subtract(days => ($dt->day_of_week - $desired_dow) % 7);
my $month_day_strp = 'DateTime::Format::Strptime'->new( pattern => '%B %d' );
my $monday = $month_day_strp->format_datetime($dt);
$dt->add(days => 6);
my $sunday = $month_day_strp->format_datetime($dt);
say "$datetime_str, $monday thru $sunday, $month_year";
}
Next time help someone else (in case this is what you're after -- I am not sure if I got your question). :-) I used the link posted by #scragar in comments.

How to extract Date Only

When using
test = new Date() ,
on doing console.log ('test') I get
"Thu Sep 12 2013 17:55:25 GMT+0545 (NPT)". I need to take only "Thu Sep 12 2013".
On doing these (new Date()).getDay() it does not work for me.
Help me out guys.
Thank you in advace !!!
How about:
test = new Date()
console.log(test.toDateString())
from: http://devdocs.io/javascript/global_objects/date/todatestring

How to parse Twitter search result with iOS NSJSONSerialization in a UITableView?

I'm having trouble parsing a Twitter search result with the built in iOS JSON parser.
NSDictionary *resultorig = [NSJSONSerialization JSONObjectWithData:self.responseData options: NSJSONReadingMutableContainers error: &err];
NSMutableArray *result = [resultorig objectForKey:#"results"];
NSLog(#"%#", result);
I'm searching Twitter with this url: http://search.twitter.com/search.json?q=%23xbox
in which prints out:
{"completed_in":0.013,"max_id":288030748135530498,"max_id_str":"288030748135530498","next_page":"?page=2&max_id=288030748135530498&q=%23xbox","page":1,"query":"%23xbox","refresh_url":"?since_id=288030748135530498&q=%23xbox","results":[{"created_at":"Sun, 06 Jan 2013 21:14:16 +0000","from_user":"momarkmagic","from_user_id":41954598,"from_user_id_str":"41954598","from_user_name":"Mark Molnar","geo":null,"id":288030748135530498,"id_str":"288030748135530498","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/342500438\/avat_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/342500438\/avat_normal.jpg","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"RT #conceptartworld: Check out Halo 4 Concept Art by Thomas Scholes! http:\/\/t.co\/JdDr40XM #xbox #illustration http:\/\/t.co\/AKjZGKgN","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:14:07 +0000","from_user":"DeJesusRaymond","from_user_id":57501625,"from_user_id_str":"57501625","from_user_name":"Raymond DeJesus\u0950","geo":null,"id":288030711229861888,"id_str":"288030711229861888","iso_language_code":"da","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3069994571\/4b2761d7571bb012354f7efd47c71eb2_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3069994571\/4b2761d7571bb012354f7efd47c71eb2_normal.jpeg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"#360 #xbox #swag #thuglife. http:\/\/t.co\/Q0mteFMA","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:13:59 +0000","from_user":"ShotzLiam_3","from_user_id":598301611,"from_user_id_str":"598301611","from_user_name":"Liam","geo":null,"id":288030679453798400,"id_str":"288030679453798400","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2866443960\/9c7349d8672774cae56aedfcc1955b31_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2866443960\/9c7349d8672774cae56aedfcc1955b31_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"#FifaaGiveaways can I have some coins #xbox","to_user":"FifaaGiveaways","to_user_id":943979888,"to_user_id_str":"943979888","to_user_name":"Free UT Giveaways"},{"created_at":"Sun, 06 Jan 2013 21:13:28 +0000","from_user":"Kuvaga","from_user_id":83686315,"from_user_id_str":"83686315","from_user_name":"Kut V. \u26a1","geo":null,"id":288030544850206720,"id_str":"288030544850206720","iso_language_code":"es","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3060318139\/af19b6a35cdf09e4f54e12835e18093e_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3060318139\/af19b6a35cdf09e4f54e12835e18093e_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"Este es el que te dije #GarcIA_MGM #Xbox http:\/\/t.co\/04N9fESH","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:13:24 +0000","from_user":"benzybk","from_user_id":275588581,"from_user_id_str":"275588581","from_user_name":"Benzy Babykutty","geo":null,"id":288030531063541762,"id_str":"288030531063541762","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3040249039\/d741b8ccfdb9d130be72eadd77471adc_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3040249039\/d741b8ccfdb9d130be72eadd77471adc_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"RT #TheSunNewspaper: The next Xbox versus the PS4, we take a look at what to expect. http:\/\/t.co\/SdFzZUYv #xbox #ps3 #gaming","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:13:13 +0000","from_user":"Lewis_Whitfield","from_user_id":53468672,"from_user_id_str":"53468672","from_user_name":"ImLatchingOntoYou.","geo":null,"id":288030482623516672,"id_str":"288030482623516672","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3008431805\/15b78d1e299047711e5ab6da0cbe3e28_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3008431805\/15b78d1e299047711e5ab6da0cbe3e28_normal.jpeg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"Completed Forza Horizon! Yay #xbox #xbox360 #forza #completed #sad #intense #best #game #ever http:\/\/t.co\/AedVwph0","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:51 +0000","from_user":"sexyboy529","from_user_id":394412198,"from_user_id_str":"394412198","from_user_name":"Sexyboy","geo":null,"id":288030392844435456,"id_str":"288030392844435456","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"RT #Sam_James96: My life is dedicated to xbox today. #xbox #GTA5","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:28 +0000","from_user":"Andreact93","from_user_id":211217957,"from_user_id_str":"211217957","from_user_name":"Andrea Gazzo","geo":null,"id":288030296312537090,"id_str":"288030296312537090","iso_language_code":"da","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1771937572\/foto_twitter_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1771937572\/foto_twitter_normal.jpg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"#xbox360 #controller #game #halo4 #games #gamers #xbox #modded #led #blue http:\/\/t.co\/bTkCuyT0","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:24 +0000","from_user":"2xMolly2xBlunt","from_user_id":259448291,"from_user_id_str":"259448291","from_user_name":"\u2665Rihanna\u2665BabyDaddy\u2665\ue420","geo":null,"id":288030277299736577,"id_str":"288030277299736577","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2956632635\/442f652a4e993f26d285e80ccb57910a_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2956632635\/442f652a4e993f26d285e80ccb57910a_normal.png","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"Time to play 2k who wanna play #RT #2K13 #XBOX","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:23 +0000","from_user":"_kdog","from_user_id":118113755,"from_user_id_str":"118113755","from_user_name":"Kirsten Thomson","geo":null,"id":288030276670611458,"id_str":"288030276670611458","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3061762292\/c8d1047d30ab44a6ee922500bce85370_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3061762292\/c8d1047d30ab44a6ee922500bce85370_normal.jpeg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"I love skyrim so much. #skyrim #Lynda #Clyde #ilovewater #xbox #sarahando http:\/\/t.co\/bM2dSIkZ","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:10 +0000","from_user":"ReeceAxten","from_user_id":405803362,"from_user_id_str":"405803362","from_user_name":"ReeceAxten\u00ae","geo":null,"id":288030220164952064,"id_str":"288030220164952064","iso_language_code":"tl","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3074248109\/0fa728912afd324e8722169c4efc3ab2_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3074248109\/0fa728912afd324e8722169c4efc3ab2_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"Fifa anyone? #xbox","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:11:02 +0000","from_user":"karlheath6","from_user_id":984240522,"from_user_id_str":"984240522","from_user_name":"Karl heath","geo":null,"id":288029933144518657,"id_str":"288029933144518657","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3007892972\/4988eca8a918c2fa1b28b48d0f0980df_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3007892972\/4988eca8a918c2fa1b28b48d0f0980df_normal.jpeg","source":"<a href="https:\/\/mobile.twitter.com">Mobile Web (M2)<\/a>","text":"RT #Retroshock316: For a chance 2 win #DirtShowdown for #Xbox, follow #Retroshock316 + RT this. Winner announced when we hit 2k followers. #Comp #Competition","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:10:45 +0000","from_user":"SWARNERx","from_user_id":244070634,"from_user_id_str":"244070634","from_user_name":"- Shell Warner\u2717","geo":null,"id":288029861786820609,"id_str":"288029861786820609","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3071322789\/ba7c82da179f851fea6a4a12ad66c0bd_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3071322789\/ba7c82da179f851fea6a4a12ad66c0bd_normal.jpeg","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"back to #xbox .#Blackops2 add me : SWARNERx . #Zombies.","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:10:26 +0000","from_user":"headshotcola","from_user_id":1064288336,"from_user_id_str":"1064288336","from_user_name":"Xbox_Headshotcola","geo":null,"id":288029783584022528,"id_str":"288029783584022528","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/sticky\/default_profile_images\/default_profile_6_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_6_normal.png","source":"<a href="http:\/\/twitter.com\/download\/android">Twitter for Android<\/a>","text":"People buy #ForzaHorizon it is such a good game best racing game you would #xbox","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:10:15 +0000","from_user":"joecurry281186","from_user_id":338231824,"from_user_id_str":"338231824","from_user_name":"Joe Curry","geo":{"coordinates":[52.092676,-1.836255],"type":"Point"},"id":288029737761263616,"id_str":"288029737761263616","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2402359791\/image_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2402359791\/image_normal.jpg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"Just deleted a load of content to install FableIII and its still running like crap #xbox #ffs","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null}],"results_per_page":15,"since_id":0,"since_id_str":"0"}
I"m trying to display the tweet text in each row of the tableview but it crashes with my current code:
NSDictionary *aTweet = [result objectAtIndex:indexPath.row];
cell.theTweet.text = [aTweet objectForKey:#"text"];
NSLog(#"%#", [aTweet objectForKey:#"text"]);
What am I doing wrong? Any help would be appreciated because I've spent almost all day trying to fix this crash. Thanks.
I figured it out finally. It was in the parsing of the tree. I was parsing it incorrectly. I needed to loop through the tree as follows:
for (NSDictionary *newDictionary in result) {
NSString *tweetNew = [newDictionary objectForKey:#"text"];
}
The JSON Response you are obtaining is valid. So in usual situations there should not be any problems in parsing it.
Personally I am a fan of SBJson for JSON parsing. Please refer this link
http://stig.github.com/json-framework/
Its good if you can map the in coming data to model classes. To map data to model classes you can make use of any KVC wrappers.
Hope this helps.