利用JSON检索值的Objective-C [英] Retrieving values from json using objective-c

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

问题描述

目前我正在试图用JSON但是和Objective-C有一点难度的工作。以下是返回的JSON

  {
    sethostname =(
    {
        封邮件=更新的Apache配置\\ nUpdating的cPanel许可证...完成更新成功\\ nBuilding全局高速缓存的cPanel ...完成。
        状态= 1;
        statusmsg =主机名更改为:a.host.name.com
        警告=(
        );
    });
}

我能够检查响应回来,关键是sethostname但是无论我怎么努力,我不能得到例如状态或statusmsg值。任何人都可以点我在正确的位置。以下是基本的code,我使用检查sethostname被返回。

  NSError * myError =零;
*的NSDictionary解析度= [NSJSONSerialization JSONObjectWithData:应对方案:NSJSONReadingMutableLeaves错误:&放大器; myError]。
的NSLog([水库说明]);
NSArray的* ARR;
ARR = [RES allKeys]
如果([ARR containsObject:@sethostname])
{
    的NSLog(@工作);
}


解决方案

当有疑问,写下你的JSON数据的结构。例如:

  {
    sethostname =(
    {
        封邮件=更新的Apache配置\\ nUpdating的cPanel许可证...完成更新成功\\ nBuilding全局高速缓存的cPanel ...完成。
        状态= 1;
        statusmsg =主机名更改为:a.host.name.com
        警告=(
        );
    });
}

(这是NeXTSTEP的属性列表格式,实际上)意味着你有一个顶级的字典。这个顶层字典包含一个名为键 sethostname ,其值是一个数组。该阵列由词典,有一组键,每个键的字典:短信息,状态,statusmsg,警告封邮件有一个字符串值,状态有一些价值, statusmsg 有一个字符串值 warns`有一个数组值:

 词典(顶层)
    sethostname(字典数组)
        字典(数组元素)
            封邮件(字符串)
            状态(数)
            statusmsg(字符串)
            警告(阵列)
                ??? (数组元素)

在认识这个结构,你的code应该是这样的:

  NSError * myError =零;
*的NSDictionary解析度= [NSJSONSerialization JSONObjectWithData:应对方案:NSJSONReadingMutableLeaves错误:&放大器; myError]。如果(!RES){// JSON解析器失败}//字典(顶层)
如果(![RES isKindOfClass:[NSDictionary的类]){
    // JSON解析器并没有回词典
}// sethostname(字典数组)
NSArray的* setHostNames = [RES objectForKey:@sethostname];//词典(数组元素)
对于(*的NSDictionary在setHostName setHostNames){
    //状态(数)
    *的NSNumber状态= [setHostName objectForKey:@状态];    // statusmsg(串)
    * NSString的statusmsg = [setHostName objectForKey:@statusmsg];    ...
}

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 =             (
        );
    });
}

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

解决方案

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 =             (
        );
    });
}

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

    …
}

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

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