我是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":""
}
答案 0 :(得分: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"]);
这可以帮助你
答案 1 :(得分:1)
您可以使用NSJSONSerialization执行此操作。
NSData *response = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];
答案 2 :(得分: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"]);
}
编辑:更新循环,更好的解析方法,因为你可以循环遍历每个单独的药物对象,如果你想在需要时将数据解析为药物对象类,那就更容易了
答案 3 :(得分: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。
答案 4 :(得分: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);
我认为这会对你有所帮助。