-1

Hi I am a beginner in ios and I am trying to get below loans array inside "id" details.

And I would like a store that details in separate array, I have tried using some code but I did not get any results.

Someone help me please.

my code:

  #import "ViewController.h"
    #import "MBProgressHUD.h"

    @interface ViewController ()
    {
        NSMutableData * webData;
        NSURLConnection * connection;
        NSMutableArray * array;
    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        array = [[NSMutableArray alloc]init];

        NSURL * url  = [NSURL URLWithString:@"http://192.168.2.104:8080/RouteTracking/maprun/Route/GetRouteListByUserId?userId=511187762371709"];

        NSURLRequest * request = [NSURLRequest requestWithURL:url];
        connection = [NSURLConnection connectionWithRequest:request delegate:self];

        if(connection)
        {
            webData = [[NSMutableData alloc]init];
        }

        [super viewDidLoad];

       // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

        [webData setLength:0];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

        [webData appendData:data];
    }

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

        NSLog(@"error is %@",[error localizedDescription]);
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

        NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];

       NSDictionary * responseString = [allDataDictionbary JSONValue];

        NSArray * allTweets = [responseString objectForKey:@"routeDetails"];

        NSLog(@"details are %@",allTweets);

        for (NSDictionary * obj in allTweets) {

           NSDictionary * act = [obj objectForKey:@"loans"];
           NSLog(@"main dictionary is %@",act);
            NSString * name = [act objectForKey:@"id"];
           [array addObject:name];
        }

        NSLog(@"act array is %@",array);
    } 

Here's the setup of my JSON:

routeDetails
    [
      -{
         id: 285,
         name: name 1,
        -loans: [
             -{
                id: 42,
                name: "Shark"
              }
         ]
       },
      -{
         id: 286,
         name: name 2,
         -loans: [
              -{
                 id: 50,
                 name: "Flipper"
               }
          ]
       }
    ]
Elydasian
  • 2,016
  • 5
  • 23
  • 41
Krish
  • 4,166
  • 11
  • 58
  • 110

2 Answers2

0
first count the number of item in the response ... and u can run loop as much as the count ...
suppose i hve this main resplonse 
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions  error:&error];
now i ll store it in tableArray = [json valueForKeyPath:@"widget_order.ordercollection"];
now make other nsmutable array which will store  details 
ex : 
nsmutablearray *yourdetilary  = [nsmutableaty alloc]init]; 
for (int i= 0 ; i< tableArray ; i++)
{
    yourdetilary = [addObject [json valueForKeyPath:@"widget_customer.customers_detail"]];
}
Indrajit Chavda
  • 361
  • 1
  • 2
  • 13
0

Consider a url : http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topTvEpisodes/json

Now load the url into data then JSON data to dictionay

let data = NSData(contentsOfURL: url!)
        let dataDict:AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil)!

Now you need to separate the data so make a object class if you require or you can just make the variables to assign data from JSON.

for details:AnyObject in movieList
                    {
                        var movieData = MovieDetails ()
                        let movie: AnyObject? = details.valueForKeyPath("title")
                        movieData.moviename = movie?.objectForKey("label") as? String
                        let movieSummary: AnyObject? = details.valueForKeyPath("summary")
                        movieData.summary = movieSummary?.objectForKey("label") as? String
                        let moviePrice: AnyObject? = details.valueForKeyPath("im:price")
                        movieData.price = moviePrice?.objectForKey("label") as? String
                        let movieRDate: AnyObject? = details.valueForKeyPath("im:releaseDate")
                        movieData.releaseDate = movieRDate?.objectForKey("label") as? String

                        // Fetching Image Url
                        var imageDict:AnyObject = details.valueForKeyPath("im:image")!
                        // println(imageDict[2])
                        let imageLarge: AnyObject! = imageDict[2]
                        movieData.imageUrl = imageLarge["label"] as? String
                        println(movieData.imageUrl)

                        var movieRights:AnyObject = details.valueForKeyPath("rights")!
                        movieData.rights = movieRights.objectForKey("label") as? String
                        println(movieData.rights)
                        moviesArray.append(movieData)
                    }

You can do it like this to get the specific data i have used array of custom class so if you run in playground it might not work directly this is FYI.

To test in playground

var url:NSURL?
var data1:NSData?

url = NSURL(string: "http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topTvEpisodes/json")
data1 = NSData(contentsOfURL: url!)

var json1: AnyObject? = NSJSONSerialization.JSONObjectWithData(data1!, options: nil, error: nil)

let parse: AnyObject? = json1?.valueForKeyPath("feed.entry.title.label")
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71