【问题标题】:How to get JSON array values in iOS如何在 iOS 中获取 JSON 数组值
【发布时间】:2016-08-29 12:32:35
【问题描述】:

我有 JSON 字典,但我想获取这个子值。像名称,部分,类别,图像,出生日期。如何获取值,主要是如何通过此位代码获取图像。如何显示图像并将位码转换为图像。请帮忙

我的 Json

[
 {
"ID": "1000710017",
"CLASS": "1",
"SECTION": "A",
"NAME": "testing",
"DOB": "123\/123\/999",
"IMAGE": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AAB0N0lEQVR4Xu29B4BdR3U+PpJWuytp1btly7ItueAOuGAwNqbYGIMN+BdCgOBACDFOQgkB0sg\.......so big code"
         }
       ]

我的代码

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
 NSLog(@"get id %@",self.uid_value);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://sixthsenseit.com/school/project/ios/profile.php"]];


//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];

//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
NSString *userUpdate =[NSString stringWithFormat:@"id=%@&",_uid_value, nil];



//Check The Value what we passed
// NSLog(@"the data Details is =%@", userUpdate);

//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];

//Apply the data to the body
[request setHTTPBody:data1];

//Create the response and Error
NSError *err;
NSURLResponse *response;

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


NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
                                                     options:kNilOptions
                                                       error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

//This is for Response
 NSLog(@"got response==%@", resSrt);
if (json)
{
NSArray *results = [json valueForKey:@"NAME"];


}


if(resSrt)
{
    NSLog(@"got response");

}

else
{
    NSLog(@"faield to connect");
}

}

【问题讨论】:

  • 你能出示你的 cdoe
  • 您从 web 服务获得格式正确的 IMAGE 链接。更改图片 URL 的格式。
  • 您面临的问题是什么?
  • @Anbu.Karthik 请检查编辑我的问题

标签: ios objective-c iphone json web-services


【解决方案1】:

不需要这个

 NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

//This is for Response
 NSLog(@"got response==%@", resSrt);
if (json)
{
NSArray *results = [json valueForKey:@"NAME"];
}


if(resSrt)
{
NSLog(@"got response");

}

else
{
NSLog(@"faield to connect");
}

简单地做这个

NSArray *json = [NSJSONSerialization JSONObjectWithData:responseData
                                                     options:kNilOptions
                                                       error:&err];
if (json.count>0) {
    self.txtName.text =json[0][@"NAME"];
     self.txtClass.text =json[0][@"CLASS"];
     self.txtSection.text =json[0][@"SECTION"];

    NSData* data = [json[0][@"IMAGE"] dataUsingEncoding:NSUTF8StringEncoding];

     self.imgBig.image= [UIImage imageWithData:data];

}

【讨论】:

  • 这一行缺少 NSArray *jsonAreay = json[@""]; ,我需要这里的答案 NSDictionary *json
  • 你在那里,我需要你的帮助,只需在这里打印你在控制台 json 中得到的内容
  • 很抱歉,因为我要回家了,紧急。所以这个问题明天解决,请明天再来。并解决我的问题。坦克你
  • @AnkurKumawat - 当然。
  • 是的,在这里打印一次 NSDictionary *json 结果
【解决方案2】:

要解析 json 尝试使用下面的代码

-(void)convertJson : (NSString *)resSrt
{
    NSArray *jsonObjectAry = [NSJSONSerialization JSONObjectWithData:[resSrt dataUsingEncoding:NSUTF8StringEncoding]
                                                          options:0 error:NULL];
         if (jsonObjectAry)
       NSLog(@"Name=>%@",jsonObjectAry[0][@"NAME"]);

}

【讨论】:

  • 为什么要增加内存,检查一次O/p是sngle数组
  • 如果它是单个对象,为什么服务器以数组形式发送。我假设,有可能得到对象列表。我只是分享了代码的和平。用户可以优化他们想要的。仅供参考:如果服务器发送空数组,“第 0 个索引将为空”。
【解决方案3】:

处理所有这些子值的一个好方法是使用像SwiftyJSON这样的第三方

您不必编写冗长的方法来解析这些值。它还处理值为nil的情况。

let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string {
  //Now you got your value
}

参考:SwiftyJSON。

【讨论】:

  • OP 将问题标记为objective-c。
  • @TejaNandamuri:ObjectiveC 也有类似的库。
  • OP主要问题是获取json中的bitCode并将其转换为图像。您的回答没有说​​明与此相关的任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
相关资源
最近更新 更多