解析json字典,数组中的数组?在更深的巢中获取钥匙的物体?

时间:2014-10-27 12:10:24

标签: ios arrays json parsing dictionary

好的愚蠢的问题,这应该是显而易见的,但我所有的谷歌搜索都没有做任何事情。

我遇到了这两种方法:

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"

我似乎无法弄清楚,嵌套让我困惑。

4 个答案:

答案 0 :(得分:2)

这是直接获取数组

中的响应值的答案
 //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"];

答案 1 :(得分:0)

如果你发布的是你的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"];

答案 2 :(得分:0)

我会非常简单地解决您的问题。请按照以下步骤进行操作

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

   }}

答案 3 :(得分:0)

一旦到达了密钥值“VM_LIST”的数组,就可以使用valueForKey(使用键值编码)来获取“ID”键的所有值的数组。要到达那个数组,只需按下去 - objectForKey:@“Data”将您带到一个带有一个键的字典“VM_LIST”。使用objectForKey:该字典上的“VM_LIST”将您带到您感兴趣的数组。所以,您只需要,

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

jsonDict是您在问题中登出的词典。