【问题标题】:Convert Json to Ios format将 Json 转换为 Ios 格式
【发布时间】:2014-07-25 12:28:42
【问题描述】:

我尝试在 iOS 中解析 json,但出现错误。 这是我从 api 得到的 json

data =     (
{
LogIn =             {
Emails = "abc@xyz`.com";
Latitude = "37.33233141";
Longitude = "-122.03121860";
Password = 123456;
};
Status = Success;
}
);

我想要所有详细信息,例如电子邮件、纬度、经度和密码以及状态。我正在尝试下面的 json 代码

loginurl= [NSURL URLWithString:string1];
NSLog(@"url is %@",string1);
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:loginurl];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

这是我的获取数据方法 -

-(void)fetchedData:(NSData *)responseData
{   
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData      options:kNilOptions error:&error];

    NSLog(@"JSON: %@", json);
    NSDictionary *data = [json objectForKey:@"data"];
    NSArray *currentConditions = [data objectForKey:@"LogIn"];

    NSDictionary *conditions = [currentConditions objectAtIndex:0];
    NSLog(@"%@",conditions);
 }

【问题讨论】:

    标签: ios json parsing jsonp


    【解决方案1】:

    首先,您的 JSON 格式无效。应该是这样的:

    {
        "data": {
            "LogIn": {
                "Emails": "abc@xyz`.com",
                "Latitude": "37.33233141",
                "Longitude": "-122.03121860",
                "Password": 123456
            },
            "Status": "Success"
        }
    }
    

    您可以使用http://jsonlint.com/ 来验证您的 JSON 数据。

    其次,

    [data objectForKey:@"LogIn"];
    

    是字典,而不是数组。

    试试:

    NSDictionary *currentConditions = [data objectForKey:@"LogIn"];
    NSLog(@"Longitude: %@", [currentConditions objectForKey@"Longitude"]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-26
      • 2011-07-04
      • 2019-09-26
      • 2018-03-06
      • 1970-01-01
      • 2018-03-11
      • 2020-08-30
      相关资源
      最近更新 更多