简体   繁体   English

解析json字典,数组中的数组? 获取更深层嵌套中的关键对象?

[英]Parse json dictionary, array within arrays? Get objects for key in deeper nests?

Okay stupid question, this should be obvious but all my googling didn't do nothing. 好的愚蠢的问题,这应该很明显,但是我所有的谷歌搜索并没有做任何事情。

I've met these two methods: 我已经遇到了以下两种方法:

myarray/dictionary = [jsonDictionary objectForKey:@"ID"];

This gets the pair for the key ID 这将获取密钥ID对

myarray/dictionary = jsonDictionary[@"COMMON"];

This omits the data within COMMON 这会忽略COMMON的数据

This is my dictionary from json, how do I get an array of all the ID keys? 这是我来自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;
            }
        );
    };
}

The proper output for the array would be 数组的正确输出是

@"VMSPE0000000083", @"VMSPE0000000093", @"VMSPE0000000096"

I can't seem to figure it out, the nesting confuses me. 我似乎无法弄清楚,嵌套使我感到困惑。

This is the answer for directly getting values of response in array 这是直接获取数组中响应值的答案

 //just give your URL instead of my URL

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL     URLWithString:@"http://http://stackoverflow.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"];

If what you posted is your JSON data, and you have it all in an NSDictionary* jsonDict, then 如果您发布的是您的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"];

I will solve your problem very simply.Please just follow the following steps 我会很简单地解决您的问题。请按照以下步骤操作

 //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"]];

   }}

Once you get to the array that's the value of the key, "VM_LIST", you can use valueForKey (which uses Key-Value coding) to get the array of all the values of the "ID" key. 到达键值“ VM_LIST”的数组后,就可以使用valueForKey(使用键值编码)来获取“ ID”键的所有值的数组。 To get to that array just work your way down -- objectForKey:@"Data" gets you to a dictionary with one key, "VM_LIST". 要进入该数组,只需按顺序进行操作-objectForKey:@“ Data”使用一个键“ VM_LIST”使您进入字典。 Using objectForKey:"VM_LIST" on that dictionary gets you to the array you're interested in. So, all you need is, 在该字典上使用objectForKey:“ VM_LIST”可以将您带到您感兴趣的数组。因此,您所需要的只是,

NSArray *array = [jsonDict[@"DATA"]["VM_LIST"] valueForKey:@"ID"];

jsonDict is the dictionary that you logged out in your question. jsonDict是您在问题中注销的字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM