0

当我在执行此行时解析我的 Json 文件时

model.userProfileObj.userFirstName = [NSString stringWithFormat:@"%@",[[responseDict valueForKey:@"Profile"] valueForKey:@"first_name"]];

Profile 是我的字典, first_name 是它的内部变量。像:

[
    {
        "Profile": {
            "id": "13",
            "user_id": "13",
            "first_name": "Myname",
            "profile_image": "13-IMG_169.png",
        }
    }
]

将以下字符串分配给 model.userProfileObj.userFirstName 但我只需要 MyName 而不是 ( 和 " 在变量中。

(
    "Myname"
)
4

3 回答 3

0
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *returnString=[[NSString alloc]initWithData:dataReceivedG encoding:NSASCIIStringEncoding];
    SBJSON *jsonParser=[[SBJSON alloc]init];
    NSDictionary *_dict=(NSDictionary *)[jsonParser objectWithString:returnString error:nil];
    NSLog(@"----%@",_dict);
    NSArray *_arrData=(NSArray *)[_dict ob
    //NSDictionary *_dictResponse=(NSDictionary *)[_objectForKey:@"profile"];
    NSLog(@"===%@",_arrData);
    for (int j=0; j<[_arrData count]; j++) 
    {
        [arrayListG addObject:[[_arrData objectAtIndex:j]valueForKey:@"first_name"]];
    }
} 

检查这个。

于 2012-11-27T11:08:01.953 回答
0

如果你有一个带有 JSON 内容的字符串,你可以使用一个正则表达式

 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Myname:\\\"([^\\\"]*)\\\"" options:0 error:NULL];
    NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];

    NSString *encodedPoints = [jsonAsString substringWithRange:[match rangeAtIndex:1]];
于 2012-11-27T11:22:49.003 回答
0

您的结果responseDict是数组格式,您将它用作字典,这就是为什么您会得到这样的结果

(
    "Myname"
)

所以只需在你的代码中做一件事..[responseDict objectAtIndex:0]

你的代码

model.userProfileObj.userFirstName = [NSString stringWithFormat:@"%@",[[[responseDict objectAtIndex:0] valueForKey:@"Profile"] valueForKey:@"first_name"]];
于 2012-11-27T13:39:04.047 回答