[英]How to parse such a json array?
我在嘗試解析以下json數組時遇到困難。 如何解析。 網絡中的其他答案似乎無法解決我的問題。
{
"status": 1,
"value": {
"details": [
{
"shipment_ref_no": "32",
"point_of_contact": {
"empid": ""
},
"products": {
"0": " Pizza"
},"status": "2"
},
{
"shipment_ref_no": "VAPL/EXP/46/14-15",
"point_of_contact": {
"empid": "60162000009888"
},
"products": {
"0": "MAIZE/CORN STARCH"
},
"status": "5"
}
]
}
}
我必須訪問每個鍵的值。
以下是我的代碼
NSString* pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSArray *argsArray = [[NSArray alloc] initWithArray:[jsonDic objectForKey:@"details"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", jsonDic[@"values"]);
這是解析整個字典的方法:
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *details = [[dataDictionary objectForKey:@"value"] objectForKey:@"details"];
for (NSDictionary *dic in details) {
NSString *shipmentRefNo = dic[@"shipment_ref_no"];
NSDictionary *pointOfContact = dic[@"point_of_contact"];
NSString *empId = pointOfContact[@"empid"];
NSDictionary *products = dic[@"products"];
NSString *zero = products[@"0"];
NSString *status = dic[@"status"];
}
NSString *pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSArray *argsArray = [[NSArray alloc] initWithArray:[jsonDic objectForKey:@"details"]];
//argsArray holds objects in form of NSDictionary.
for(NSDictionary *response in argsArray) {
//String object
NSLog(@"%@", [response valueForKey:@"shipment_ref_no"]);
//Dictionary object
NSLog(@"%@", [[response objectForKey:@"point_of_contact"] valueForKey:@"empid"]);
//String object
NSLog(@"%@", [response valueForKey:@"status"]);
//Dictionary object
NSLog(@"%@", [[response objectForKey:@"products"] valueForKey:@"0"]);
}
我相信您一定應該要求服務器開發人員更新響應格式。
另外,您始終可以使用Model類來解析數據。 請檢查一下如何將NSDictionary轉換為自定義對象 。
是的,我正在使用此網站來檢查我的json響應。
你可以解析...
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dictValue = [[NSDictionary alloc] initWithDictionary:[jsonDic objectForKey:@"value"]];
NSArray *arrDetails = [[NSArray alloc] initWithArray:[dictValue objectForKey:@"details"]];
for (int i=0; i<arrDetails.count; i++)
{
NSDictionary *dictDetails=[arrDetails objectAtIndex:i];
NSDictionary *dictContact = [[NSDictionary alloc] initWithDictionary:[dictDetails objectForKey:@"point_of_contact"]];
NSDictionary *dictProduct = [[NSDictionary alloc] initWithDictionary:[dictDetails objectForKey:@"products"]];
}
編輯:以下答案在javascript中!
您可以使用以下方式解析json數據:
var array = JSON.parse(data);
然后您將獲得如下所示的所有信息:
var refno = array["value"]["details"][0]["shipment_ref_no"];
NSDictionary *response = //Your json
NSArray *details = response[@"value"][@"details"]
等。很容易
如下更新代碼。 您正在嘗試從頂層讀取details
數組,而在您的數據中則是使用value
鍵。 因此,您應該閱讀值dict,並在其中閱讀details數組。
NSString* pendingResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [pendingResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSArray *argsArray = [[NSArray alloc] initWithArray:[valueDict objectForKey:@"details"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", jsonDic[@"values"]);
我認為您的問題是您有:
NSLog(@"keys = %@", jsonDic[@"values"]);
但是應該是:
NSLog(@"keys = %@", jsonDic[@"value"]);
以下是解析JSON數組的代碼。 我曾經從文件中解析JSON數組,但是您也可以使用響應鏈接來做到這一點。
// using file
NSString *str = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [[NSData alloc]initWithContentsOfFile:str];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *dictValues = [[NSMutableDictionary alloc]initWithDictionary:[dict valueForKey:@"value"]];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[dictValues valueForKey:@"details"] copyItems:YES];
NSLog(@"Array Details :- %@",array);
// using url
NSURL *url = [NSURL URLWithString:@"www.xyz.com"]; // your url
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *dictValues = [[NSMutableDictionary alloc]initWithDictionary:[dict valueForKey:@"value"]];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[dictValues valueForKey:@"details"] copyItems:YES];
NSLog(@"Array Details :- %@",array);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.