IOS:Dictionary返回null值/ Data

时间:2015-12-09 05:13:49

标签: ios json dictionary nsdictionary

在我的项目中我正在使用JSON解析来显示这个我创建的数据2 NSDictionary字典1返回成功响应但是当我在第二个字典中调用它时它返回Null值。 我正在分享我的代码,请帮我纠正它

-(void)hdata
{



    NSString *post = [NSString stringWithFormat:@"data[Users][ref_id]=%@&api_key=bf45c093e542f057c123ae7d6",refidstr];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://192.168.0.20/hspCh/api/user_diagnose_list"]];

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

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

    [request setHTTPBody:postData];

    NSString *str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"str : %@",str);



    NSDictionary *dict6 = [self cleanJsonToObject:responseData];
    NSLog(@"str : %@",dict6);    <=======it gives correct responce



    diagnosisdict = [dict6 objectForKey:@"data[Users][ref_id]"];

    [diagnosisdict setValue:refidstr forKey:@"data[Users][ref_id]"];
    NSLog(@" for ref id =%@ ,  data is= %@",refidstr, diagnosisdict);  <======Gives null responce

}

在控制台中输出以上内容

2015-12-09 10:34:16.935 HcH[2841:32315] str : {"response":200,"diagnoses":[{"DiagnosesHospitals":{"hospital_id":"3341","id":"163075","discharges":"100.00","charge_amt":"1200.00","total_amt":"1500.00","medicare_amt":"1200.00"},"Diagnoses":{"diagnosis_name":"TRANSIENT ISCHEMIA"}}]}
2015-12-09 10:34:16.935 HcH[2841:32315] str : {
    diagnoses =     (
                {
            Diagnoses =             {
                "diagnosis_name" = "TRANSIENT ISCHEMIA";
            };
            DiagnosesHospitals =             {
                "charge_amt" = "1200.00";
                discharges = "100.00";
                "hospital_id" = 3341;
                id = 163075;
                "medicare_amt" = "1200.00";
                "total_amt" = "1500.00";
            };
        }
    );
    response = 200;
}
2015-12-09 10:34:16.936 HcH[2841:32315]  for ref id =3341 ,  data is= (null)

3 个答案:

答案 0 :(得分:2)

替换此

diagnosisdict = [dict6 objectForKey:@"data[Users][ref_id]"];

diagnosisdict = [[[dict6 objectForKey:@"diagnoses"] objectAtIndex:0] objectForKey:@"DiagnosesHospitals"];

答案 1 :(得分:1)

dict6中没有“数据[用户] [ref_id]”的条目。

顺便说一下,将该字符串用作键是完全荒谬的。此上下文中的键只是一个字符串,而不是字典中的一系列访问。

看起来你想要的是dict6[@"data"][@"Users"][@"ref_id"]

但是,这也会失败,因为dict6中没有“数据”条目。

您的cleanJSONToObject:方法可能会删除“数据”键的值。但是,根据您的声明,打印dict6的值会给出正确的响应,我假设您实际想要访问的是“DiagnosesHospitals”中“id”的值,因为它听起来最像“ REF_ID”。

根据调试器的输出,好像你有一个字典(dict6),其中“diagnoses”键指向一个带有两个字典的数组。

所以,您可以使用dict6[diagnoses][0][@"DiagnosesHospitals"][@"id"]访问“id”字段,假设“id”是一个字符串(我不确定为什么它周围没有引号)。

答案 2 :(得分:1)

你可以得到的东西:

NSDictionary * diagnoses = dict6[@"diagnoses"];
NSString * diagnosis_name = diagnoses[0][@"Diagnoses"][@"diagnosis_name"];
NSDictionary * diagnosisdict = diagnoses[0][@"DiagnosesHospitals"];
NSString * hospital_id = diagnosisdict[@"hospital_id"];