-1

I got JSON dictionary, but i want to get in this son values. like NAME, SECTION,CLASS,IMAGE,DOB. how to get values and mostly how to get image by this bit code. how to show image and convert bit code to image. please help

my Json

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

my 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");
}

}
Ankur Kumawat
  • 91
  • 2
  • 12

3 Answers3

2

no need of this

 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 Do this

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];

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

To parse the json try to use below code

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

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Mohanraj S K
  • 657
  • 4
  • 16
  • 1
    why you increase the memory, check once O/p it is sngle array – Anbu.Karthik Aug 29 '16 at 12:51
  • If its a single object , why server send as array. I assumed , there may possible to get list of objects . And am just shared a peace of code. user can optimize what they want. FYI: If server send empty array, "0th index will be null". – Mohanraj S K Aug 29 '16 at 13:00
0

A good way to handle all these son values is to use a third party like SwiftyJSON

You don't have to write lengthy methods to parse these values. Also it handles the case where the value is nil.

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

Reference: SwiftyJSON.

Shobhit C
  • 828
  • 10
  • 15