1

URL から取得した JSON データがいくつかあります。私が書いたコードは、JSON をダウンロードして解析するのに問題なく動作しますが、特にデータが別のサブ要素として含まれている場合、必要な方法でアクセスできないようです。

JSON 形式は次のとおりです。

{
    address = "<null>";
    city = "<null>";
    country = UK;
    "country_code" = GB;
    daylight = 1;
    for = daily;
    items =     (
                {
            asr = "5:22 pm";
            "date_for" = "2013-7-1";
            dhuhr = "1:01 pm";
            fajr = "2:15 am";
            isha = "11:47 pm";
            maghrib = "9:24 pm";
            shurooq = "4:39 am";
        }
    );
    latitude = "50.9994081";
    link = "http://muslimsalat.com/UK";
    longitude = "0.5039011";
    "map_image" = "http://maps.google.com/maps/api/staticmap?center=50.9994081,0.5039011&sensor=false&zoom=13&size=300x300";
    "postal_code" = "<null>";
    "prayer_method_name" = "Muslim World League";
    "qibla_direction" = "119.26";
    query = "51.000000,0.500000";
    state = "<null>";
    timezone = 0;
    title = UK;
    "today_weather" =     {
        pressure = 1020;
        temperature = 14;
    };
}

(これらはイスラムの祈りの時間です。)

これまでの私のObjective-Cはこれです:

-(CLLocationCoordinate2D) getLocation{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    return coordinate;
}

//class to convert JSON to NSData
- (IBAction)getDataFromJson:(id)sender {
    //get the coords:
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"*dLatitude : %@", latitude);
    NSLog(@"*dLongitude : %@",longitude);

    //load in the times from the json
    NSString *myURLString = [NSString stringWithFormat:@"http://muslimsalat.com/%@,%@/daily/5.json", latitude, longitude];
    NSURL *url = [NSURL URLWithString:myURLString];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];

    if(jsonData != nil)
    {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        NSArray *jsonArray = (NSArray *)result; //convert to an array
        if (error == nil)
            NSLog(@"%@", result);
            NSLog(@"%@", jsonArray);
            for (id element in jsonArray) {
                NSLog(@"Element: %@", [element description]);

            }
    }
}

このコードを実行すると、要素名のリスト (address, city, countryなど) しか出力されません。itemsが与えられますが、その子要素は与えられません。これが私がコードに求めているものであることを理解しています:

for (id element in jsonArray) {
    NSLog(@"Element: %@", [element description]);
}

しかし、次のステップに進む方法がわかりません。

私が必要とする唯一のデータ値は、実際には時間そのものです (so、、、items>asrなどitems>dhuhr)。

これらの値自体を取得して、操作できる値として保存するにはどうすればよいですか?

ありがとうございました!

4

3 に答える 3

3

NSArray *jsonArray = (NSArray *)result; //convert to an array

これは「変換」しません。result実際にNSArray. そしてこの場合、それは嘘です。

あなたのコードは現在、JSON で返される辞書のキーのリストを出力しているだけです。アイテムのリストを取得するには、これを試してください (これは配列であるため、複数のエントリが存在する可能性があることに対処する必要があります)。

NSDictionary *result = [NSJSONSerialization ...

for (NSDictionary *itemDict in result[@"items"]) {
    NSLog(@"item: %@", itemDict);
}

次に、時間を抽出できます。

于 2013-07-01T06:52:35.800 に答える
1

次の方法で情報を抽出できます。

 NSError* error = nil;
 NSDictionary *userInfo; //your main data

 if([NSJSONSerialization class])
  userInfo = [NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];

//to extract items
  NSDictionary *items = [[[userInfo objectForKey:@"items"] JSONValue] objectAtIndex:0];
于 2013-07-01T06:55:25.543 に答える