开发者

How to read the JSON value on console in iphone

开发者 https://www.devze.com 2023-03-13 07:21 出处:网络
i havethe following json value in console: {\"TokenID\":\"kuiHigen21\",\"isError\":false,\"ErrorMessage\":\"\",\"Result\":[{\"UserId\":\"153\",\"FirstName\":\"Rocky\",\"LastName\":\"Yadav\",\"Email\"

i have the following json value in console:

{"TokenID":"kuiHigen21","isError":false,"ErrorMessage":"","Result":[{"UserId":"153","FirstName":"Rocky","LastName":"Yadav","Email":"rocky@itg.com","ProfileImage":null,"ThumbnailImage":null,"DeviceInfoId":"12"}],"ErrorCode":900}  

this is my server api :@"http://192.168.0.68:91/JourneyMapperAPI?RequestType=Login" //api takes 5 parameters . when i post data to server api values are posted to server and i get the ab开发者_开发技巧ove response in json format.

i want to parse the above the JSON value that i get in the response and save in sqlite database.

i am doing this code to parse the above JSON value:

 -(void)connectionDidFinishLoadingNSURLConnection *)connection 
{ 
  NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] lengthwebData length] encoding:NSUTF8StringEncoding];     
     NSLog(@"%@",loginStatus); 

self.webData = nil; 

      SBJSON *parser =[[SBJSON alloc]init]; 

     NSURLRequest *request = [NSURLRequest requestWithURLNSURL URLWithString"http://192.168.0.68:91/JourneyMapperAPI?RequestType=Login.json"]]; 

         NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

  // Get JSON as a NSString from NSData response 

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

  //NSDictionary *object = [parser objectWithString:json_string error:nil]; 
  // parse the JSON response into an object   
// Here we're using NSArray since we're parsing an array of JSON status objects 

NSArray *statuses = [parser objectWithString:json_string error:nil];    


for (NSDictionary *status in statuses)  
 {     
// You can retrieve individual values using objectForKey on the status NSDictionary  
   // This will print the tweet and username to the console  

NSLog(@"%@ - %@", [status objectForKey"Login"],[status objectForKey"LoginKey"]);  

  [connection release];   [webData release]; 

} 


You should check out some of the JSON parsers, my personal favourite is json-framework. After you've included one of them in your project, where you've got your JSON response from your server:

// Get JSON as a NSString from NSData response 

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

NSDictionary *result = [json_string JSONValue];
NSArray *statuses = [result objectForKey:@"Result"];

which will return your array of results (where each object in the array is an NSDictionary).


You can save this to a database with the help of a model class, Result

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

NSDictionary *result = [json_string JSONValue];
NSArray *values = [result objectForKey:@"Result"];
NSMutableArray *results = [[NSMutableArray alloc] init];

for (int index = 0; index<[values count]; index++) {
   NSMutableDictionary * value = [values objectAtIndex:index];
   Result * result = [[Result alloc] init];
   result.UserId = [value objectForKey:@"UserId"];
   result. FirstName = [value objectForKey:@"FirstName"];
   ...

   [results addObject:result];
   [result release]; 
}

use the array of results to save it to the database.

for (int index = 0; index<[results count]; index++) {
   Result * result = [results objectAtIndex:index];

   //save the object variables to database here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消