簡體   English   中英

如何從JSON響應中獲取數組對象值?

[英]How to get array object values from JSON response?

我有這種格式的json響應,請查看此。我想為每個地址獲取經度和緯度值。

{
    "message":"success",
    "data":
    {
        "docs":
        [
            {
                "_id":"573d8eca67c7f172cc88387e",
                "user":
                {
                    "phone":"8510932519)/+",
                    "image":"",
                    "name":"Niraj@%"
                },
                "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 
    }
}

我需要為每個地址加一點經久。 我正在使用此代碼獲取地址,但是如何獲取位置數組中0和1索引的經度和經度?

  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"];

      }

嘗試這個

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"];
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM