我有JSON字典,但我希望得到这个儿子的价值观。比如NAME,SECTION,CLASS,IMAGE,DOB。如何获取值,主要是如何通过此位代码获取图像。如何显示图像并将位代码转换为图像。请帮忙
[
{
"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");
}
}
答案 0 :(得分:2)
不需要这个
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");
}
Simpley执行此操作
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];
}
答案 1 :(得分:0)
解析json尝试使用下面的代码
-(void)convertJson : (NSString *)resSrt
{
NSArray *jsonObjectAry = [NSJSONSerialization JSONObjectWithData:[resSrt dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
if (jsonObjectAry)
NSLog(@"Name=>%@",jsonObjectAry[0][@"NAME"]);
}
答案 2 :(得分:0)
处理所有这些儿子价值观的好方法是使用像SwiftyJSON
这样的第三方您不必编写冗长的方法来解析这些值。它还处理值为nil
的情况。
let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string {
//Now you got your value
}
参考:SwiftyJSON。