【问题标题】:How to separate JSON response in iphone如何在 iPhone 中分离 JSON 响应
【发布时间】:2013-08-29 06:58:54
【问题描述】:

我有一个应用程序,其中有这样的 json 响应。 {"success":"true","message":"You have logged in","pserial":"1"} 和我正在与 ":" 分开。 我正在获取类似pSerial:"1"} 的数据,但我只想要1 值。

NSURL *url = [NSURL URLWithString:strUrl];
NSData *respData = [NSData dataWithContentsOfURL:url];
NSString *strResp = [[NSString alloc]initWithData:respData encoding:NSUTF8StringEncoding];
NSString *approvalString = [[strResp componentsSeparatedByString:@":"] objectAtIndex:3];
NSLog(@"pSerial:%@",approvalString);

【问题讨论】:

标签: iphone ios json


【解决方案1】:

例如:

SBJsonParser *jsonPar = [[SBJsonParser alloc] init];
NSError *error = nil;
NSArray *jsonObj = [jsonPar objectWithString:jsonString error:&error];


id jsonObj = [jsonPar objectWithString:jsonString error:&error];

if ([jsonObj isKindOfClass:[NSDictionary class]])
    // treat as a dictionary, or reassign to a dictionary ivar
else if ([jsonObj isKindOfClass:[NSArray class]])
    // treat as an array or reassign to an array ivar.

然后获取值:

NSMutableArrary *userMutArr = [NSMutableArray array];

for (NSDictionary *dict in jsonObj)
{
    User *userObj = [[[User alloc] init] autorelease];
    [userObj setFirstName:[dict objectForKey:@"firstName"]];
    [userObj setLastName:[dict objectForKey:@"lastName"]];
    [userObj setAge:[[dict objectForKey:@"age"] intValue]];
    [userObj setAddress:[dict objectForKey:@"address"]];
    [userObj setPhoneNumbers:[dict objectForKey:@"phoneNumber"]];

    [userMutArr addObject:userObj];
}

希望你能理解。并阅读一些文件。它会帮助你。

【讨论】:

    【解决方案2】:

    您似乎需要 JSON 解析。在这里,您需要的是 JSON 解析,而不是从 JSON 响应中分离数据。 JSON 是数据的格式,其中数据以键值对格式设置。您可以使用 "Key" 获取任何对象的 "Value"

    你的前两行是正确的。

    NSURL *url = [NSURL URLWithString:strUrl];
    NSData *respData = [NSData dataWithContentsOfURL:url];
    

    现在,您可以像这样解析 JSON 响应:

    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:respData options:kNilOptions error:&error];
    NSString *pSerial = [json objectForKey:@"pserial"];
    

    这将从您的回复中为您提供 “pserial” 的值。同样,您可以获得 "success""message" 的值。您可以使用此行进行检查:

    NSLog(@"pserial :: %@",pserial);
    

    【讨论】:

      【解决方案3】:

      您需要解析 JSON 响应字符串,您可以使用任何 JSON 解析器,例如:

      https://github.com/stig/json-framework/

      在你的代码中做:

      NSString *strResp = [[NSString alloc]initWithData:respData encoding:NSUTF8StringEncoding];
      
      NSDictionary *ResponseDictionary = [strResp JSONValue];
      
      NSString * pSerial = (NSString*)[ResponseDictionary objectForKey:@"pserial"];
      

      【讨论】:

        【解决方案4】:

        不要用“:”分隔,只需使用 JSONValue 你的响应就像

         //     {"success":"true","message":"You have logged in","pserial":"1"} 
        
         //  with SBJsonParser parse your object like this
        
         NSDictionary *responseJson = [YOUR-OBJECT JSONValue];
        

        注意:不要忘记添加Json头文件

        【讨论】:

          【解决方案5】:

          最好使用任何开源 Json 解析器

          这是一个堆栈帖子比较不同的 Json Parser for iOS

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多