简体   繁体   中英

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

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

This omits the data within COMMON

This is my dictionary from json, how do I get an array of all the ID keys?

{
    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

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. To get to that array just work your way down -- objectForKey:@"Data" gets you to a dictionary with one key, "VM_LIST". Using objectForKey:"VM_LIST" on that dictionary gets you to the array you're interested in. So, all you need is,

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

jsonDict is the dictionary that you logged out in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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