使用objective-c从json中检索值 [英] Retrieving values from json using objective-c

查看:23
本文介绍了使用objective-c从json中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 json 和 Objective-c,但是遇到了一些困难.以下是返回的json

I am currently trying to work with json and objective-c however having a bit of difficulty. The following is the json that is being returned

{
    sethostname =     (
    {
        msgs = "Updating Apache configuration\nUpdating cPanel license...Done. Update succeeded.\nBuilding global cache for cpanel...Done";
        status = 1;
        statusmsg = "Hostname Changed to: a.host.name.com";
        warns =             (
        );
    });
}

我能够检查响应是否返回并且密钥是 sethostname 但是无论我尝试什么我都无法获得例如 status 或 statusmsg 的值.任何人都可以指出我在正确的位置.以下是我用来检查是否返回 sethostname 的基本代码.

I am able to check that the response is coming back and the key is sethostname however no matter what I try I cannot get for example the value of status or statusmsg. Can anyone point me in the right location. The following is basic code I am using to check that sethostname is returned.

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&myError];
NSLog([res description]);
NSArray *arr;
arr = [res allKeys];
if ([arr containsObject:@"sethostname"])
{
    NSLog(@"worked");
}

推荐答案

如有疑问,请写下 JSON 数据的结构.例如:

When in doubt, write down the structure of your JSON data. For example:

{
    sethostname =     (
    {
        msgs = "Updating Apache configuration\nUpdating cPanel license...Done. Update succeeded.\nBuilding global cache for cpanel...Done";
        status = 1;
        statusmsg = "Hostname Changed to: a.host.name.com";
        warns =             (
        );
    });
}

(实际上是 NeXTSTEP 属性列表格式)意味着您有一个顶级字典.这个顶级字典包含一个名为 sethostname 的键,它的值是一个数组.这个数组由字典组成,每个字典都有一组键:msgs, status, statusmsg, warns.msgs 有一个字符串值,status 有一个数字值,statusmsg 有一个字符串值warns` 有一个字符串值数组值:

(which is in NeXTSTEP property list format, actually) means that you have a top-level dictionary. This top-level dictionary contains a key called sethostname whose value is an array. This array is comprised of dictionaries, each dictionary having a set of keys: msgs, status, statusmsg, warns. msgs has a string value, status has a number value, statusmsg has a string value,warns` has an array value:

dictionary (top-level)
    sethostname (array of dictionaries)
        dictionary (array element)
            msgs (string)
            status (number)
            statusmsg (string)
            warns (array)
                ??? (array element)

理解了这个结构后,你的代码应该是这样的:

Having understood this structure, your code should look like:

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&myError];

if (!res) { // JSON parser failed }

// dictionary (top-level)
if (![res isKindOfClass:[NSDictionary class]]) {
    // JSON parser hasn't returned a dictionary
}

// sethostname (array of dictionaries)
NSArray *setHostNames = [res objectForKey:@"sethostname"];

// dictionary (array element)
for (NSDictionary *setHostName in setHostNames) {
    // status (number)
    NSNumber *status = [setHostName objectForKey:@"status"];

    // statusmsg (string)
    NSString *statusmsg = [setHostName objectForKey:@"statusmsg"];

    …
}

这篇关于使用objective-c从json中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆