Showing posts with label AniruddhSinh Json. Show all posts
Showing posts with label AniruddhSinh Json. Show all posts

Wednesday, 28 January 2015

Json Parser

Json parsar

Here json Example using the NXJson parser..

There are Two Method To Do This ..... It Is Given Below :

Method ( 1 ) :-

-: First Call Method In Viewdidload and Write This Code In Method :-



NSHTTPURLResponse *responce = nil;
    
    NSString *urlstring = [NSString stringWithFormat:@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=20/json"];
    
    NSURL *urldata = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLRequest *urlrequest = [[NSURLRequest alloc]initWithURL:urldata];
    
    NSData *mydata = [NSURLConnection sendSynchronousRequest:urlrequest returningResponse:&responce error:nil];
    
    NSMutableDictionary *maindic = [NSJSONSerialization JSONObjectWithData:mydata options:NSJSONReadingMutableContainers error:nil];

Then Write Coding To Fetch Your Data Depend Your Need .... Example Is Given Below

NSMutableDictionary *feeddic = [maindic objectForKey:@"feed"];
    
    
    NSMutableArray *mainarr = [feeddic objectForKey:@"entry"];
    
    for (int i = 0; i < mainarr.count; i++)
    {
        
        Container *data = [[Container allocinit];
        
        NSMutableDictionary *subdic = [mainarr objectAtIndex:i];
        
        NSMutableDictionary *secnamedic = [subdic objectForKey:@"im:name"];
        
        NSString *stringsecname = [secnamedic objectForKey:@"label"];
        
        data.strsecname = stringsecname;
        
        NSMutableArray *imgarr = [subdic objectForKey:@"im:image"];
        
        for (int j = 0; j < imgarr.count; j++)
        {
            NSMutableDictionary *secsubdic = [imgarr objectAtIndex:0];
            
            NSString *stringimg = [secsubdic objectForKey:@"label"];

            data.strimg=stringimg;
          
        }
      
        [arrobj addObject:data];

    }
  
    [tblobj reloadData];


Method ( 2) :-


1. First put the json folder in your new project Or Exiting project ...(that given below) in this Folder  include the AsiHttp,json,singlton,Staticclass,Singleton folder .

  Click here for Download Jason file :-   A

this file must import in your bundel.....


Json Folder contain This class files


2. json folder contain all the non ARC file so set the flag (-fno-objc-arc ) in compile sources.
     (go to Build Phases --> compile sources ---> set flag to -fno-objc-arc)

3. add following  framework in your project

    1  -  security.framework
    2  -  systemConfiguratin.framework
    3 -   libz.dylib
    4  -  MobileCoreServices.framework
    5  -  CFNetwork.framework
    6  -  libxml2.dylib

4. inser new  path in header searchpath.

(go Build Setting -> Search path-->Header Search Paths)


/usr/include/libxml2 

and  finally build your project..(cmd +b). (if error occur than see and check again all step).
 NOTE :- if you get some syntax error then double click on them and fix -it .
so no error occur than ...
Then start you project to working json file..

First of all Import These Three files.

#import "Singleton.h"
#import "HttpQueue.h"
#import "NXJsonParser.h"


Then After In ViewDidLoad method register a notification.

-(void)viewDidLoad
{ 
 [super viewDidLoad];
  

[[NSNotificationCenter defaultCenterremoveObserver:self name:@"1" object:nil];
    
    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(getAllAPIDataCompletion:) name:@"1" object:nil];
    
    [[NSNotificationCenter defaultCenterremoveObserver:self name:@"-1" object:nil];
    
    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(getAllAPIDataFail:) name:@"-1" object:nil];


}

send request using httpQueue class and get response to given Method. 


-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    NSString *myreqstr=[NSString stringWithFormat:@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=10/json"]; // using The Raywendrlich json api for demo
    
    [[HttpQueue sharedSingletongetItems:myreqstr :1];
    

}

When request is successful get all data This method.


-(void ) getAllAPIDataCompletion : (NSNotification *)notification {
    
    NSDictionary *dict = [notification userInfo];
    
    ASIHTTPRequest *res = [dict objectForKey:@"index"];
    
    NXJsonParser * parser = [[NXJsonParser allocinitWithData:[res responseData]];
    
    id result = [parser parse:nil ignoreNulls:NO];
    
    NSDictionary *maindic = (NSDictionary *)result;
    NSDictionary *feeddic = [maindic objectForKey:@"feed"];
    NSArray *entryarr = [feeddic objectForKey:@"entry"];
    
    
    for (int i=0; i<entryarr.count; i++)
    {
        Container *contain = [[Container allocinit];
        
        
        NSDictionary *subdic = [entryarr objectAtIndex:i];
        NSDictionary *namedic = [subdic objectForKey:@"im:name"];
        NSString *strname = [namedic objectForKey:@"label"];
        
        contain.strname = strname;
        
        NSArray *imgarr = [subdic objectForKey:@"im:image"];
        for (int j=0; j<imgarr.count; j++)
        {
            NSDictionary *imgdic = [imgarr objectAtIndex:j];
            NSString *strimg = [imgdic objectForKey:@"label"];
            
            contain.strimg = strimg;
            
        }
        
        [myarr addObject:contain];
    }

    [tblobj reloadData];
}





And  Fail the reason of wrong url and network fail then call this method ..


-(void ) getAllAPIDataFail : (NSNotification *)notification {
    
    NSDictionary *dict = [notification userInfo];
    
    ASIHTTPRequest *res = [dict objectForKey:@"index"];
    
    NXJsonParser * parser = [[NXJsonParser allocinitWithData:[res responseData]];
    
    id result = [parser parse:nil ignoreNulls:NO];
    
    NSLog(@"result=%@",result);
    

}

Run This project and get following out put if you get error please see above the step again .
Output in Log.



Well, This time to Get the useful data in this all Data . In this result first `{} `contain (NSDictionary ) and `( )`Or `[ ]`contain  (NSArray). if you want to get Activity`s value from the loans Then you have to implement This Step by Step.
Here in the Data `{` indicate Dictionary so we hold This Dictionary in NSDictionary .


 For Api :- 

http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=10/json


    NSDictionary *maindic = (NSDictionary *)result;


    NSDictionary *feeddic = [maindic objectForKey:@"feed"];

    NSArray *entryarr = [feeddic objectForKey:@"entry"];
    
    
    for (int i=0; i<entryarr.count; i++)

   {

    Container *contain = [[Container allocinit];     // create object of Nsobject(Container)
        
        
        NSDictionary *subdic = [entryarr objectAtIndex:i]; 

        NSDictionary *namedic = [subdic objectForKey:@"im:name"];

        NSString *strname = [namedic objectForKey:@"label"];
        
        contain.strname = strname;                              // fetch data in container
        
        NSArray *imgarr = [subdic objectForKey:@"im:image"];

        for (int j=0; j<imgarr.count; j++)

        {

            NSDictionary *imgdic = [imgarr objectAtIndex:j];

            NSString *strimg = [imgdic objectForKey:@"label"];
            
            contain.strimg = strimg;                                    // fetch data in container
            
        }
        
        [myarr addObject:contain];
    }
    
    
    [tblobj  reloadData];



Note :- In the Method of Table view new Object is needed to Show the Data.

ex :-


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Container *data = [myarr objectAtIndex:indexPath.row];
    
    MyCell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.lbl.text = data.strname;

    cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:                                                     [NSURL URLWithString:data.strimg]]];
    
    return cell;


}


I think Its May be Very Helpful to You.Thanks!!