解析json字典,数组中的数组?在更深的巢穴中获取钥匙对象?
Posted
技术标签:
【中文标题】解析json字典,数组中的数组?在更深的巢穴中获取钥匙对象?【英文标题】:Parse json dictionary, array within arrays? Get objects for key in deeper nests? 【发布时间】:2014-10-27 12:10:23 【问题描述】:好吧,愚蠢的问题,这应该很明显,但我所有的谷歌搜索都没有做任何事情。
我遇到过这两种方法:
myarray/dictionary = [jsonDictionary objectForKey:@"ID"];
这将获取密钥 ID 对
myarray/dictionary = jsonDictionary[@"COMMON"];
这省略了COMMON
中的数据
这是我的 json 字典,如何获取所有 ID 键的数组?
COMMON =
CATEGORY = computing;
"NUM_PER_PAGE" = 0;
"PAGE_NO" = 0;
"REQUEST_DATE" = 201410271757;
"RESULT_CD" = 0000;
"RESULT_MSG" = SUCCESS;
"SVC_ID" = 7;
TARGET = "list(VM)";
;
DATA =
"VM_LIST" = (
"@SVC_ID" = 7;
ID = VMSPE0000000083;
"MACHIN_STATUS_DESC" = "[150748]success:virtual machine power on";
"MEM_SIZE_MB" = 1024;
"OS_NAME" = "CentOS_6.4_en_64";
"PURPOSE_NM" = "Service_Default";
"SERVER_STATUS_MSG" = "VM running";
"USVC_DESC" = "7/Running, No Change r/hurhurhur, Inc.";
"VCPU_CNT" = 2;
"VIRT_TYPE_DESC" = "Para Virtualization";
"VM_ALIAS" = CV00900000083;
"VM_OPER_DESC" = "Power On";
"VNIC_CNT" = 1;
,
"@SVC_ID" = 7;
ID = VMSPE0000000093;
"MACHIN_STATUS_DESC" = "[150749]success:virtual machine reboot";
"MEM_SIZE_MB" = 2048;
"OS_NAME" = "Gentoo _2011-0 _en_64";
"PURPOSE_NM" = "Service_Default";
"SERVER_STATUS_MSG" = "VM running";
"USVC_DESC" = "7/Running, No Change r/hurhurhur, Inc.";
"VCPU_CNT" = 1;
"VIRT_TYPE_DESC" = "Para Virtualization";
"VM_ALIAS" = CV00900000093;
"VM_OPER_DESC" = Reboot;
"VNIC_CNT" = 1;
,
"@SVC_ID" = 7;
ID = VMSPE0000000096;
"MACHIN_STATUS_DESC" = "[163023]success:virtual machine running";
"MEM_SIZE_MB" = 1024;
"OS_NAME" = "OpenSuse_12.1_en_64";
"PURPOSE_NM" = "Service_Default";
"SERVER_STATUS_MSG" = "VM running";
"USVC_DESC" = "7/Running, No Change r/hurhurhur, Inc.";
"VCPU_CNT" = 2;
"VIRT_TYPE_DESC" = "Para Virtualization";
"VM_ALIAS" = CV00900000096;
"VM_OPER_DESC" = "Vm Initialization";
"VNIC_CNT" = 1;
);
;
数组的正确输出是
@"VMSPE0000000083", @"VMSPE0000000093", @"VMSPE0000000096"
我似乎无法弄清楚,嵌套让我感到困惑。
【问题讨论】:
尝试NSDicationary *dictionary = jsonDictionary[@"DATA"];
然后[dictionary valueForKeyPath:@"VM_LIST.ID"];
在寻找“特殊”方式之前,请先采用“即插即用”方式。遍历字典数组,提取每个 ID 值,并将其填充到 NSMutableArray 中。
如果你看不懂 JSON,去 json.org 学习 JSON 语法。只需 5-10 分钟即可学会。
【参考方案1】:
这是直接在数组中获取响应值的答案
//just give your URL instead of my URL
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://http://***.com/questions/26587283/parse-json-dictionary-array-within-arrays-get-objects-for-key-in-deeper-nests/26589961#26589961"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
//step by step getting values from Array[copy the Dictionary values to Array] format
NSMutableArray *array = [[jsonArray objectForKey:@"COMMON"] mutableCopy];
NSString *strCAT = [NSString stringWithFormat:@"%@",[array valueForKey:@"CATEGORY"]];
NSString *strNUM = [NSString stringWithFormat:@"%@",[array valueForKey:@"NUM_PER_PAGE"]];
NSString *strPAGE = [NSString stringWithFormat:@"%@",[array valueForKey:@"PAGE_NO"]];
NSString *strREQUEST = [NSString stringWithFormat:@"%@",[array valueForKey:@"REQUEST_DATE"]];
NSString *strRESULT = [NSString stringWithFormat:@"%@",[array valueForKey:@"RESULT_CD"]];
NSString *strRESULT_MS = [NSString stringWithFormat:@"%@",[array valueForKey:@"RESULT_MSG"]];
NSString *strSVC = [NSString stringWithFormat:@"%@",[array valueForKey:@"SVC_ID"]];
NSString *strTAR = [NSString stringWithFormat:@"%@",[array valueForKey:@"TARGET"]];
//Getting values from Mutiple Dictionary of Array inside the Dictionary.We are going to have all values of SVC_ID,ID,MACHIN_STATUS_DESC,MEM_SIZE_MB,OS_NAME,PURPOSE_NM,SERVER_STATUS_MSG,USVC_DESC,VCPU_CNT,VM_ALIAS,VIRT_TYPE_DESC,VM_OPER_DESC,VNIC_CNT in seperate Array directly using valueForKeyPath.
NSDictionary *dictValue = [jsonArray valueForKey:@"DATA"];
NSMutableArray *arraySVC_ID = [dictValue valueForKeyPath:@"VM_LIST.@SVC_ID"];
NSMutableArray *arrayID = [dictValue valueForKeyPath:@"VM_LIST.ID"];
NSMutableArray *arrayMACHIN_STATUS_DESC = [dictValue valueForKeyPath:@"VM_LIST.MACHIN_STATUS_DESC"];
NSMutableArray *arrayMEM_SIZE_MB = [dictValue valueForKeyPath:@"VM_LIST.MEM_SIZE_MB"];
NSMutableArray *arrayOS_NAME = [dictValue valueForKeyPath:@"VM_LIST.OS_NAME"];
NSMutableArray *arrayPURPOSE_NM = [dictValue valueForKeyPath:@"VM_LIST.PURPOSE_NM"];
NSMutableArray *arraySERVER_STATUS_MSG = [dictValue valueForKeyPath:@"VM_LIST.SERVER_STATUS_MSG"];
NSMutableArray *arrayUSVC_DESC = [dictValue valueForKeyPath:@"VM_LIST.USVC_DESC"];
NSMutableArray *arrayVCPU_CNT = [dictValue valueForKeyPath:@"VM_LIST.VCPU_CNT"];
NSMutableArray *arrayVM_ALIAS = [dictValue valueForKeyPath:@"VM_LIST.VM_ALIAS"];
NSMutableArray *arrayVIRT_TYPE_DESC = [dictValue valueForKeyPath:@"VM_LIST.VIRT_TYPE_DESC"];
NSMutableArray *arrayVM_OPER_DESC = [dictValue valueForKeyPath:@"VM_LIST.VM_OPER_DESC"];
NSMutableArray *arrayVNIC_CNT = [dictValue valueForKeyPath:@"VM_LIST.VNIC_CNT"];
【讨论】:
【参考方案2】:如果您发布的是您的 JSON 数据,并且您将其全部保存在 NSDictionary* jsonDict 中,那么
NSDictionary* commonDict = jsonDict [@"COMMON];
NSString* category = commonDict [@"CATEGORY"];
NSDictionary* dataDict = jsonDict [@"DATA];
NSArray* vmList = dataDict [@"VM_LIST"];
NSDictionary* firstVM = vmList [0];
NSString* firstVMID = firstVM [@"ID"];
【讨论】:
【参考方案3】:我会很简单地解决你的问题。请按照以下步骤操作
//just give your URL instead of my URL
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
//You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER.If you do this clearly you can get the correct results.
//After that it depends upon the json format whether it is DICTIONARY or ARRAY
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
//Step by Step getting values from Dictionary Format
NSString *strCATEGORY = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"CATEGORY"]];
NSString *strNUM_PER_PAGE = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"NUM_PER_PAGE"]];
NSString *strPAGE_NO = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"PAGE_NO"]];
NSString *strREQUEST_DATE = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"REQUEST_DATE"]];
NSString *strRESULT_CD = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"RESULT_CD"]];
NSString *strRESULT_MSG = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"RESULT_MSG"]];
NSString *strSVC_ID = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"SVC_ID"]];
NSString *strTARGET = [NSString stringWithFormat:@"%@",[[jsonArray valueForKey:@"COMMON"]valueForKey:@"TARGET"]];
NSMutableArray *arrayDict = [[jsonArray valueForKey:@"DATA"]valueForKey:@"VM_LIST"];
for(int i=0;i<[arrayDict count];i++)
NSString *strSVC_ID = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"@SVC_ID"]];
NSString *strID = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"ID"]];
NSString *strMACHIN_STATUS_DESC = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"MACHIN_STATUS_DESC"]];
NSString *strMEM_SIZE_MB = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"MEM_SIZE_MB"]];
NSString *strOS_NAME = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"OS_NAME"]];
NSString *strPURPOSE_NM = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"PURPOSE_NM"]];
NSString *strSERVER_STATUS_MSG = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"SERVER_STATUS_MSG"]];
NSString *strUSVC_DESC = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"USVC_DESC"]];
NSString *strVCPU_CNT = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"VCPU_CNT"]];
NSString *strVM_ALIAS = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"VM_ALIAS"]];
NSString *strVIRT_TYPE_DESC = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"VIRT_TYPE_DESC"]];
NSString *strVM_OPER_DESC = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"VM_OPER_DESC"]];
NSString *strVNIC_CNT = [NSString stringWithFormat:@"%@",[[arrayDict objectAtIndex:i]valueForKey:@"VNIC_CNT"]];
【讨论】:
【参考方案4】:一旦您获得作为键值的数组“VM_LIST”,您就可以使用 valueForKey(它使用键值编码)来获取包含“ID”键的所有值的数组。要访问该数组,只需按您的方式进行操作——objectForKey:@"Data" 将您带到带有一个键“VM_LIST”的字典。在该字典上使用 objectForKey:"VM_LIST" 可以将您带到您感兴趣的数组。所以,您所需要的只是,
NSArray *array = [jsonDict[@"DATA"]["VM_LIST"] valueForKey:@"ID"];
jsonDict 是您在问题中注销的字典。
【讨论】:
以上是关于解析json字典,数组中的数组?在更深的巢穴中获取钥匙对象?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用新的 Swift 3 和 Alamofire 解析 JSON(字典和数组)