具有objectAtIndex的多维数组

时间:2013-08-01 12:08:47

标签: objective-c arrays multidimensional-array

我尝试从内容中读取多维数组:

[
  {
    "content" : {
      "parcel" : {
        "ServiceParcelEventPostalCode" : "1005",
        "ConsigneePostalCode" : "1220  (292)",
        "ParcelTypeDescription" : "Paket",
        "ReferenceIdentcode" : "1017348010239480212208",
        "CustomerShipmentNr" : "N\/A",
        "Identcode" : "1017348010239480212208",
        "ReferenceNumber" : "D1Lmp8TPN_1",

..........等

我正试图让ServiceParcelEventPostalCode无法访问:

    NSString *urlString = @"http://www.post.at/sendungsverfolgung4.php";

    NSURL *url = [NSURL URLWithString:urlString];

    // URL-Request-Objekt erstellen
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"GET"];

    NSMutableData *body = [NSMutableData data];
    NSString *postWerte = [NSString stringWithFormat:@"id=%@", self.textfeld.text];

    [body appendData:[postWerte dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    // Abfrage ans Web senden und Rückgabe in Variable speichern
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    const char *convert = [returnString UTF8String];
    NSString *responseString = [NSString stringWithUTF8String:convert];
    NSMutableArray *meinErgebnis = [responseString JSONValue];

    NSString *wert1 = [NSString stringWithFormat:@"%@",[[[meinErgebnis objectAtIndex:2] objectAtIndex:0] objectForKey:@"ServiceParcelEventPostalCode"]];

1 个答案:

答案 0 :(得分:0)

只需遵循JSON的结构即可。 JSON中的[]表示数组,{}表示字典。如果您的示例中的JSON是您的responseString

NSMutableArray *jsonArray = [responseString JSONValue];
NSString *postalCode = nil;
if (jsonArray.count)
{
    NSDictionary *elementDict = [jsonArray objectAtIndex:0];
    NSDictionary *contentDict = [elementDict objectForKey:@"content"];
    NSDictionary *parcelDict = [contentDict objectForKey:@"parcel"];
    postalCode = [parcelDict objectForKey:@"ServiceParcelEventPostalCode"];
}