2016-05-24 6 views
0

Ich habe eine JSON Antwort in diesem Format bitte sieh dir das an. Ich möchte lat und lange Werte für jede Adresse bekommen.Wie Array-Objektwerte aus JSON-Antwort erhalten?

{ 
    "message":"success", 
    "data": 
    { 
     "docs": 
     [ 
      { 
       "_id":"573d8eca67c7f172cc88387e", 
       "user": 
       { 
        "phone":"8510932519)/+", 
        "image":"", 
        "name":"[email protected]%" 
       }, 
       "distance":18825, 
       "bookingNumber":"42aopy2dyry8", 
       "bookingType":0, 
       "paymentMode":"Card", 
       "tip":0, 
       "estimatedFare":51.1, 
       "estimatedDuration":"2364", 
       "created":"2016-05-18T14:49:31.231Z", 
       "stop2": 
       { 
        "address":"Malviya Nagar, New Delhi, Delhi 110017, India", 
        "location":[28.533519700000003,77.21088569999999] 
       }, 
       "stop1": 
       { 
        "address":"Ansari Nagar East, New Delhi, Delhi 110029, India", 
        "location": 
        [ 
         28.566540099999997, 
         77.2098409 
        ] 
       }, 
       "destination": 
       { 
        "address":"Saket, New Delhi, Delhi 110017, India", 
        "location": 
        [ 
         28.524578699999996, 
         77.206615 
        ] 
       }, 
       "currentLocation": 
       { 
        "address":"26, Ashok MargJ Block, Pocket J, Sector 18", 
        "location": 
        [ 
         28.568437, 
         77.32404 
        ] 
       } 
      } 
     ], 
     "total":1, 
     "limit":8, 
     "page":":1", 
     "pages":1 
    } 
} 

ich brauche für jede Adresse lat und lange zu erhalten. Ich verwende diesen Code für die Adresse, aber wie bekomme ich Lat und Long für 0 und 1 Index in Location Array?

dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"]; 


    NSArray *IDArray = [dictionary objectForKey:@"docs"]; 
    for (NSDictionary *Dict in IDArray) 
    { 

     NSMutableDictionary *temp = [NSMutableDictionary new]; 
     [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"]; 

     NSString *booknumber = [Dict objectForKey:@"bookingNumber"]; 
     if([booknumber length] != 0) 
      [temp setObject:booknumber forKey:@"bookingNumber"]; 

     NSMutableDictionary *stp1 = [Dict objectForKey:@"stop1"]; 

     if ([[stp1 allKeys] containsObject:@"address"]) { 

      [temp setObject:[stp1 objectForKey:@"address"] forKey:@"address"]; 
     } 

     NSMutableDictionary *stp2 = [Dict objectForKey:@"stop2"]; 

     if ([[stp2 allKeys] containsObject:@"address"]) { 

      [temp setObject:[stp2 objectForKey:@"address"] forKey:@"address1"]; 
     } 



     NSMutableDictionary *currentloc = [Dict objectForKey:@"currentLocation"]; 

     if ([[currentloc allKeys] containsObject:@"address"]) { 

      [temp setObject:[currentloc objectForKey:@"address"] forKey:@"address1"]; 

     } 
+0

allererst hinzufügen richtige Tags ... iOS .. aber ist es in schnellem oder Obj-C ... andere Sache ist, Ihren Code schreiben ... –

+0

@Marco Sir dies ist im Wörterbuch-Format, alle die Antwort –

+0

, wo Sie schlug –

Antwort

1

try this

NSMutableDictionary *stp1 = [Dict objectForKey:@"stop1"]; 

    if ([[stp1 allKeys] containsObject:@"address"]) { 

     [temp setObject:[stp1 objectForKey:@"address"] forKey:@"address"]; 

     // take one Temp array for fetch lat and long 

    NSArray *tempstp1 = [stp1 objectForKey:@"location"]; 
     [temp setObject:[tempstp1 objectAtIndex:0] forKey:@"latitude"]; 
     [temp setObject:[tempstp1 objectAtIndex:1] forKey:@"longitude"]; 
    } 

    NSMutableDictionary *stp2 = [Dict objectForKey:@"stop2"]; 

    if ([[stp2 allKeys] containsObject:@"address"]) { 

     [temp setObject:[stp2 objectForKey:@"address"] forKey:@"address"]; 

     // take one Temp array for fetch lat and long 

    NSArray *tempstp2 = [stp2 objectForKey:@"location"]; 
     [temp setObject:[tempstp2 objectAtIndex:0] forKey:@"latitude"]; 
     [temp setObject:[tempstp2 objectAtIndex:1] forKey:@"longitude"]; 
    } 
+0

ohh bro Entschuldigung ich wurde vergessen, Array Agian hinzuzufügen, danke seine Arbeit für mich –

+0

@sandepeptomar - haha ​​ha, bereits erwähnt du die Antwort auch bro, ('Ich bin in der Lage, Adresse Namen zu bekommen, aber wie werde ich lat und lang für 0 und 1 Index in Standort-Array ") –

+0

jawohl mein Fehler haha, danke für Ihre Unterstützung Herr –

Verwandte Themen