由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSDictionaryM objectAtIndex:] 2

时间:2014-12-25 14:11:11

标签: objective-c arrays json

我从webservice获取数据,该数据在我的桌面计算机上本地发布。这是我的json response.i我正在以数组名称json数组的形式获取此数据。在这个数组中数据正在正常,但是当我将数据转换为数组时,它就是上面的例外。 下面是我获取数据的示例代码。 }

-(void) retrievedata
{

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.3:8080/GetEmployees.svc/json/employees"]];  // this is your request url

    [request setHTTPMethod:@"GET"];

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     // here parsing the array

    NSArray *firstArry=[[jsonArray objectAtIndex:0]objectForKey:@"GetAllEmployeesMethodResult"];// getting error in this line
}

以下是我的回复:

{
    GetAllEmployeesMethodResult =     (
                {
            firstentry = 5;
            secondentry = "SANTE (PVT) LIMITED";
        },
                {
            firstentry = 8;
            secondentry = "ALLIANCE PHARMACEUTICALS (PVT) LTD";
        },
                {
            firstentry = 9;
            secondentry = "BARRETT HODGSON PAKISTAN (PVT) LTD.";
        },
                {
            firstentry = 7;
            secondentry = "GETZ PHARMA PAKISTAN (PVT) LTD";
        },
                {
            firstentry = 3;
            secondentry = "ICI PAKISTAN LTD";
        },
                {
            firstentry = 21;
            secondentry = "ADAMJEE PHARMACEUTICALS (PVT) LTD.";
        },
                {
            firstentry = 12;
            secondentry = "BROOKES PHARMACEUTICAL LABORATORIES (PAK.) LTD.";
        },
                {
            firstentry = 4;
            secondentry = "STIEFEL LABORATORIES PAKISTAN (PVT) LTD";
        },
                {
            firstentry = 1;
            secondentry = "GENIX PHARMA (PVT) LTD";
        },

    );
}

1 个答案:

答案 0 :(得分:1)

您的回复不是数组。试试这个:

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     

NSArray *firstArry=[jsonArray objectForKey:@"GetAllEmployeesMethodResult"];