2013-01-03 80 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