【问题标题】:How can I parse the JSON string output from HTTP POST in iOS如何在 iOS 中解析来自 HTTP POST 的 JSON 字符串输出
【发布时间】:2013-04-05 07:01:00
【问题描述】:

我使用 POST 方法将一些参数发送到我的服务器,并从那里得到我的响应并将其存储在字符串“serverResponse”中。这是我存储在字符串中的服务器响应

{ 
    "carIdentifier":[
        {"carId":"91"},
        {"carId":"93"},
        {"carId":"114"},
        {"carId":"117"}
    ]
}

我有单独的 json 解析代码,但我不知道如何将其作为我的 json 解析代码的输入。 以下是我的json解析代码

-(void)getcarList:(NSString *)serverResponse
{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSObject *recommendedcarJSON = nil;
        NSData *recommendedcarsData = [NSData dataWithContentsOfURL:[NSURL URLWithString:serverResponse]];
        NSLog(@"%@",recommendedcarsData);
        if(recommendedcarsData)
            recommendedcarJSON = [NSJSONSerialization JSONObjectWithData:recommendedBooksData options:NSJSONReadingMutableContainers error:nil];

        if(!recommendedcarsData){
            NSLog(@"Error with JSON Serialization");
        } else{
            [self saveRecommendedcarData:(NSDictionary *)recommendedcarJSON];
        }
    } );
}

在 saveRecommendedcarData 方法中,我正在创建一个 plist 并保存此解析的数据。如何将我的服务器响应字符串作为 json 解析的输入??

【问题讨论】:

  • @Bhargavi 你能告诉我答案吗?
  • 您显示的 json 是您的 serverResponse 字符串,对吗?
  • 是的。这是我用来从服务器 NSString * serverOutput= [[NSString alloc]initWithData:returnData encoding:NSASCIIStringEncoding] 获取响应的代码; NSLog(@"%@",[NSString stringWithFormat:@"%@",serverOutput]);

标签: ios json nsarray nsdictionary


【解决方案1】:
NSError *jsonError = nil;
id allValues = [NSJSONSerialization JSONObjectWithData:[serverResponse dataUsingEncoding:NSUTF8StringEncoding]
                                               options:0
                                                 error:&jsonError];

if(jsonError!=nil)
    NSLog(@"Json_Err: %@",jsonError);

NSArray *carIdentifier = [(NSDictionary*)allValues objectForKey:@"carIdentifier"];
NSLog(@"%@",carIdentifier);

for(NSDictionary *dict in carIdentifier)
        NSLog(@"carId: %@",[dict valueForKey:@"carId"]);

试试这个。

更新

-(void)getcarList:(NSString *)serverResponse
{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSError *jsonError = nil;
        id allValues = [NSJSONSerialization JSONObjectWithData:[serverResponse dataUsingEncoding:NSUTF8StringEncoding]
                                                   options:0
                                                     error:&jsonError];

        if(jsonError!=nil)
            NSLog(@"Json_Err: %@",jsonError);

        NSArray *carIdentifier = [(NSDictionary*)allValues objectForKey:@"carIdentifier"];
        NSLog(@"%@",carIdentifier);

        for(NSDictionary *dict in carIdentifier)
                NSLog(@"carId: %@",[dict valueForKey:@"carId"]);

        } );
}

【讨论】:

  • 您是直接使用 serverResponse 还是使用您推荐的carsData 对象?查看更新的答案尝试使用这种方式..
  • 在数据发送部分(使用post)有一个条件来检查serverresponse是否为null。 If(serverResponse!=NULL) { // 我在这里添加了你的代码 }
  • YUP ..BHARGAVI..U R A LIFE SAVER ..Working
  • :你知道如何将文本字段或 uisearch 栏添加到 UICollectioview 控制器标题,就像这个问题 stackoverflow.com/questions/5902035/…
【解决方案2】:

如果您想从日期中获取价值,请使用以下代码:

for(i=0 ; i < myDic.count; i++)
{
  NSLog(@"%@",[[myDic objectForKey:@"carIdentifier"] objectAtIndex:i] objectForKey:@"carId"])
}

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    相关资源
    最近更新 更多