2013-01-03 79 views
0

我是iPhone開發新手。我得到了JSON作爲響應如何從json中檢索數據

如何單獨從下面的JSON字符串的藥用價值:

{"arrCDrugEntity": 
    [ 
     { 
     "DrugID":1, 
     "Drug":"Benadril", 
     "Quantity":"", 
     "Comment":"", 
     "FunctionStatus":false, 
     "ResultString":"", 
     "ErrorString":"" 
     }, 
     { 
     "DrugID":2, 
     "Drug":"Dcold", 
     "Quantity":"", 
     "Comment":"", 
     "FunctionStatus":false, 
     "ResultString":"", 
     "ErrorString":"" 
     }, 
     { 
     "DrugID":3, 
     "Drug":"Dolo", 
     "Quantity":"", 
     "Comment":"", 
     "FunctionStatus":false, 
     "ResultString":"", 
     "ErrorString":"" 
     }, 
     { 
     "DrugID":4, 
     "Drug":"Paracitamol", 
     "Quantity":"", 
     "Comment":"", 
     "FunctionStatus":false, 
     "ResultString":"", 
     "ErrorString":"" 
     }, 
     { 
     "DrugID":5, 
     "Drug":"Panadol", 
     "Quantity":"", 
     "Comment":"", 
     "FunctionStatus":false, 
     "ResultString":"", 
     "ErrorString":"" 
     }, 
     { 
     "DrugID":6, 
     "Drug":"Pudin Hara", 
     "Quantity":"", 
     "Comment":"", 
     "FunctionStatus":false, 
     "ResultString":"", 
     "ErrorString":"" 
     } 
    ], 
    "FunctionStatus":true, 
    "UserID":-1, 
    "DeliveryAddress":"", 
    "ResultString":"", 
    "ErrorString":"" 
} 
+0

你試過了嗎? –

+0

是的,我試過.... – THOMAS

+0

顯示您的代碼... –

回答

1

在您的課堂上輸入SBJson.h並使用JSONValue方法將json字符串轉換爲字典。

NSDictionary *dict = [yourJsonString JSONValue]; 
    NSArray *arr = [dict valueForKey:@"arrCDrugEntity"]; 
    NSMutableArray *drugArray = [[NSMutableArray alloc] init]; 
    for (NSDictionary *drug in arr) { 
     [drugArray addObject:[drug valueForKey:@"Drug"]]; 
    } 
    NSLog(@"drugArray:%@",drugArray); 

我認爲這會對您有所幫助。

1
NSString *[email protected]"http://your_web_server/your_file...."; 
NSURL *url=[NSURL URLWithString:str]; 
     NSData *data=[NSData dataWithContentsOfURL:url]; 
     NSError *error=nil; 
     id *response=[NSJSONSerialization JSONObjectWithData:data options: 
           NSJSONReadingMutableContainers error:&error]; 

NSLog("Your JSON Object: %@ Or Error is: %@, response, error); 

以下爲retrive JSON數據

Dictionary *json = [myString JSONValue]; 

// Get the objects you want, e.g. output the second item's client id 
NSArray *items = [json valueForKeyPath:@"arrCDrugEntity"]; 
NSLog(@" client Id : %@", [[items objectAtIndex:1] objectForKey:@"clientId"]); 

代碼可能這個幫助你

+0

一步一步跟隨引用此網址:http://goo.gl/95Gbw –

+0

當處理來自互聯網的數據時,您不應該使用'... WithContentsOfURL:'。由於它同步運行,它會阻止你的主線程,並且只有下載完所有數據後才能進行用戶交互。這是非常糟糕的做法。並且你將iOS中的原生JSON實現('NSJSONSerialization')與第三方庫的代碼('SBJSON','NSString'沒有名爲'JSONValue'的代碼)混合起來 –

1

您可以使用NSJSONSerialization來做到這一點。

NSData *response = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding]; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err]; 
1

首先把你的JSON通過JSON格式粘貼之前,更易於閱讀,使用本網站

http://jsonformatter.curiousconcept.com/

其次,找到好的JSON解析器,我個人使用SBJSON,發現這裏

http://stig.github.com/json-framework/

一旦你下載了,很容易解析,這樣,下面的例子

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"drugData" ofType:@"json"]; 
NSData *myData = [NSData dataWithContentsOfFile:filePath]; 
NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; 
NSDictionary *MainJSON = [responseString JSONValue]; 

NSArray *array = [MainJSON valueForKey:@"arrCDrugEntity"]; 

for(int i = 0; i < [array count]; i++) 
    { 
     NSDictionary *temp = [array objectAtIndex:i]; 
     NSLog(@"%@", [temp valueForKey:@"Drug"]); 
    } 

編輯:更新循環,解析它的更好的辦法,因爲意味着你可以通過每個個體化用藥對象,如果你想將數據解析到藥物的對象類,如果你需要

1
所以更容易循環

使用此代碼

#import "JSONKit.h" 

NSDictionary *dictionary = [stringData objectFromJSONString]; 
NSArray *arrayOfDrugs=[NSArray alloc] initWithArray:[dictionary valueForKey:@"arrCDrugEntity"]; 

for (NSDictionary *drugDic in arrayOfDrugs) 
{ 
    NSLog(@"drug id is :%@",[drugDic valueForKey:@"DrugID"]); 
    NSLog(@"drug is :%@",[drugDic valueForKey:@"Drug"]); 
    NSLog(@"Quantity is :%@",[drugDic valueForKey:@"Quantity"]); 
    NSLog(@"Comment is :%@",[drugDic valueForKey:@"Comment"]); 
    NSLog(@"FunctionStatus is :%i",[[drugDic valueForKey:@"FunctionStatus"] intValue]); 
    NSLog(@"ResultString is :%@",[drugDic valueForKey:@"ResultString"]); 
    NSLog(@"ErrorString is %@",[drugDic valueForKey:@"ErrorString"]); 
} 

簡單JSON Files ,但你必須在構建設置JSON文件禁用ARC。

+0

請不要不使用* ANCIENT * SBJSON類。 –

+0

你可以禁用弧來建立設置http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – Warewolf

+0

謝謝你的幫助。它的工作Hercules – THOMAS