2011-06-14 48 views
0

閱讀控制檯的JSON值我已經在控制檯下面的JSON值:如何在iphone

{"TokenID":"kuiHigen21","isError":false,"ErrorMessage":"","Result":[{"UserId":"153","FirstName":"Rocky","LastName":"Yadav","Email":"[email protected]","ProfileImage":null,"ThumbnailImage":null,"DeviceInfoId":"12"}],"ErrorCode":900} 

這是我的服務器API:@「http://192.168.0.68:91/JourneyMapperAPI?RequestType =登錄「 // api需要5個參數。 當我發佈數據到服務器的api值發佈到服務器,我得到了以json格式的上述響應。

我想解析上述JSON值,我得到的響應和保存在sqlite數據庫。

我這樣做代碼分析上面的JSON值:

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

} 
+1

什麼問題? – deanWombourne 2011-06-14 11:00:12

+0

hi @deanWombourne,這是我的JSON文件apitest [18318:207] {ErrorCode = 900; ErrorMessage =「」;結果=({DeviceInfoId = 12; Email =「[email protected]」; FirstName = Rocky; LastName = Yadav; ProfileImage =「」; ThumbnailImage =「」; UserId = 153;}); TokenID = 59nUniliob; isError = 0; }請你解釋一下如何解析上面的JSON文件,並將值保存在sqlite數據庫中 – Rocky 2011-06-15 04:43:10

回答

0

可以將此保存到一個模型類的幫助數據庫,結果

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

使用結果的陣列將其保存到數據庫中。

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

    //save the object variables to database here 
} 
+0

嗨,感謝您的幫助,但現在我是問題請幫忙 – Rocky 2011-06-17 08:55:21

+0

@搖滾,告訴您的問題.. – 2011-06-20 05:38:29

1

你應該檢查出一些JSON解析器的,我個人最喜歡的是json-framework。當您已經包含在你的項目中,你有你的JSON響應從服務器其中之一:

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

將返回搜索結果的數組(其中數組中的每個對象是一個NSDictionary)。

+0

Hi @Daniel,這是我的JSON文件apitest [18318:207] { ErrorCode = 900; ErrorMessage =「」; 結果=( { DeviceInfoId = 12; 電子郵件= 「[email protected]」; 姓=岩石; 名字=亞達夫; ProfileImage = 「 」; ThumbnailImage =「 」; 用戶ID = 153 ; } ); TokenID = 59nUniliob; isError = 0; } 您可以請解釋我如何解析上述JSON文件並將值保存在sqlite數據庫 – Rocky 2011-06-14 11:39:54

+0

@Rocky那麼,使用JSON框架,它將類別方法'JSONValue'添加到NSString,所以只需調用該方法你的JSON對象。我編輯了我的答案,使其更清晰。不能幫你直接插入sqlite數據庫。 – 2011-06-14 11:54:01

+0

'JSONKit'贏得勝利!堅如磐石,方式快得多... – 2011-06-14 12:13:25