0

我尝试从内容中读取多维数组作为请求:

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

.......... ETC

我是,试图在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"]];
4

1 回答 1

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"];
}
于 2013-08-01T15:05:30.553 回答