NSString到字典?或者到json然后到字典?获取字符串的键对象

时间:2014-10-27 08:49:28

标签: ios arrays json parsing dictionary

我有一个STRING,然后我需要它作为json所以我可以解析它并获取键的对象。

例如,我需要将数组中ID的所有键对象显示在表中。 我怎么用字符串来做呢?

建议?

这里是字符串:

{
  "COMMON": {
    "RESULT_CD": "0000",
    "RESULT_MSG": "SUCCESS",
    "CATEGORY": "computing",
    "SVC_ID": "7",
    "TARGET": "list(VM)",
    "REQUEST_DATE": "201410271741",
    "PAGE_NO": 0,
    "NUM_PER_PAGE": 0
  },
  "DATA": {
    "VM_LIST": [
      {
        "@SVC_ID": "7",
        "ID": "VMSPE0000000083",
        "VM_ALIAS": "CV00900000083",
        "VM_OPER_DESC": "Power On",
        "MACHIN_STATUS_DESC": "[150748]success:virtual machine power on",
        "USVC_DESC": "7/Running, No Change r/hurhurhur, Inc.",
        "PURPOSE_NM": "Service_Default",
        "VIRT_TYPE_DESC": "Para Virtualization",
        "SERVER_STATUS_MSG": "VM running",
        "VCPU_CNT": "2",
        "MEM_SIZE_MB": "1024",
        "VNIC_CNT": "1",
        "OS_NAME": "CentOS_6.4_en_64"
      },
      {
        "@SVC_ID": "7",
        "ID": "VMSPE0000000093",
        "VM_ALIAS": "CV00900000093",
        "VM_OPER_DESC": "Reboot",
        "MACHIN_STATUS_DESC": "[150749]success:virtual machine reboot",
        "USVC_DESC": "7/Running, No Change r/hurhurhur, Inc.",
        "PURPOSE_NM": "Service_Default",
        "VIRT_TYPE_DESC": "Para Virtualization",
        "SERVER_STATUS_MSG": "VM running",
        "VCPU_CNT": "1",
        "MEM_SIZE_MB": "2048",
        "VNIC_CNT": "1",
        "OS_NAME": "Gentoo _2011-0 _en_64"
      },
      {
        "@SVC_ID": "7",
        "ID": "VMSPE0000000096",
        "VM_ALIAS": "CV00900000096",
        "VM_OPER_DESC": "Vm Initialization",
        "MACHIN_STATUS_DESC": "[163023]success:virtual machine running",
        "USVC_DESC": "7/Running, No Change r/hurhurhur, Inc.",
        "PURPOSE_NM": "Service_Default",
        "VIRT_TYPE_DESC": "Para Virtualization",
        "SERVER_STATUS_MSG": "VM running",
        "VCPU_CNT": "2",
        "MEM_SIZE_MB": "1024",
        "VNIC_CNT": "1",
        "OS_NAME": "OpenSuse_12.1_en_64"
      }
    ]
  }
}

编辑:

以下是我能够谷歌。

NSDictionary *JSON =
                 [NSJSONSerialization JSONObjectWithData: [response dataUsingEncoding:NSUTF8StringEncoding]
                                                 options: NSJSONReadingMutableContainers
                                                   error:nil];

但是,将字符串NSLog与JSON NSLog !!

进行比较

字符串:

{
  "COMMON": {
    "RESULT_CD": "0000",
    "RESULT_MSG": "SUCCESS",
    "CATEGORY": "computing",
    "SVC_ID": "7",
    "TARGET": "list(VM)",
    "REQUEST_DATE": "201410271757",
    "PAGE_NO": 0,
    "NUM_PER_PAGE": 0
  },
  "DATA": {
    "VM_LIST": [
      {
        "@SVC_ID": "7",
        "ID": "VMSPE0000000083",
        "VM_ALIAS": "CV00900000083",
        "VM_OPER_DESC": "Power On",
        "MACHIN_STATUS_DESC": "[150748]success:virtual machine power on",
        "USVC_DESC": "7/Running, No Change r/hurhurhur, Inc.",
        "PURPOSE_NM": "Service_Default",
        "VIRT_TYPE_DESC": "Para Virtualization",
        "SERVER_STATUS_MSG": "VM running",
        "VCPU_CNT": "2",
        "MEM_SIZE_MB": "1024",
        "VNIC_CNT": "1",
        "OS_NAME": "CentOS_6.4_en_64"
      },
      {
        "@SVC_ID": "7",
        "ID": "VMSPE0000000093",
        "VM_ALIAS": "CV00900000093",
        "VM_OPER_DESC": "Reboot",
        "MACHIN_STATUS_DESC": "[150749]success:virtual machine reboot",
        "USVC_DESC": "7/Running, No Change r/hurhurhur, Inc.",
        "PURPOSE_NM": "Service_Default",
        "VIRT_TYPE_DESC": "Para Virtualization",
        "SERVER_STATUS_MSG": "VM running",
        "VCPU_CNT": "1",
        "MEM_SIZE_MB": "2048",
        "VNIC_CNT": "1",
        "OS_NAME": "Gentoo _2011-0 _en_64"
      },
      {
        "@SVC_ID": "7",
        "ID": "VMSPE0000000096",
        "VM_ALIAS": "CV00900000096",
        "VM_OPER_DESC": "Vm Initialization",
        "MACHIN_STATUS_DESC": "[163023]success:virtual machine running",
        "USVC_DESC": "7/Running, No Change r/hurhurhur, Inc.",
        "PURPOSE_NM": "Service_Default",
        "VIRT_TYPE_DESC": "Para Virtualization",
        "SERVER_STATUS_MSG": "VM running",
        "VCPU_CNT": "2",
        "MEM_SIZE_MB": "1024",
        "VNIC_CNT": "1",
        "OS_NAME": "OpenSuse_12.1_en_64"
      }
    ]
  }
}

JSON:

{
    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;
            }
        );
    };
}

json缺少了东西!!

那好吗???

2 个答案:

答案 0 :(得分:0)

首先在数组中的字符串上方存储,然后您可以使用以下内容将其转换为jsonobject

[NSJSONSerialization JSONObjectWithData:[yourArray dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL]

答案 1 :(得分:0)

您可以使用名为NSJSONSerialization的内置iOS功能将NSString转换为Json格式。因为你必须将NSString转换为NSData。

// jsonString = your string
NSError *err;
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id objectChecker = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];

if ([objectChecker isKindOfClass:[NSArray class]])
{
    //your object is NSArray.
}
else if([objectChecker isKindOfClass:[NSDictionary class]])
{
    //your object is NSDictionary.
}
else 
{
    //your string doesn't contain proper json format.
}

也许这会对你有所帮助。 :)