-1

我是 iPhone 开发的新手。我得到了json回应。

如何将Drug值与下面的 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":""
}
4

5 回答 5

1
NSString *str=@"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);

以下代码用于检索 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"]);

愿这对你有帮助

于 2013-01-03T10:40:20.693 回答
1

您可以使用NSJSONSerialization来执行此操作。

NSData *response = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];
于 2013-01-03T10:40:34.333 回答
1

在粘贴之前先将你的 JSON 通过一个 JSON 格式化程序,更容易阅读,使用这个网站

http://jsonformatter.curiousconcept.com/

其次,找到好的JSON解析器,我个人使用SBJSON,找到here

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

编辑:更新循环,更好的解析方式,这意味着您可以循环遍历每个单独的药物对象,如果您想将数据解析为药物对象类,则更容易(如果需要)

于 2013-01-03T10:40:45.713 回答
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 文件 ,但您必须在 JSON 文件的构建设置中禁用 ARC。

于 2013-01-03T10:42:59.260 回答
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);

我想这会对你有所帮助。

于 2013-01-03T11:09:44.140 回答