如何使用JSON框架和Objective-C / iPhone / Xcode解析嵌套的JSON对象? [英] How to parse nested JSON objects with JSON framework and Objective-C/iPhone/Xcode?

查看:127
本文介绍了如何使用JSON框架和Objective-C / iPhone / Xcode解析嵌套的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON框架编写iPhone原生应用程序。

I'm writing an iPhone native app using the JSON framework.

我的应用程序正在使用JSON访问Web服务。我们发送的JSON数据有嵌套对象,下面是提供数据的示例:

My app is accessing web services using JSON. The JSON data we send has nested objects, below is an example of the data served up:

{
    "model": {
        "JSONRESPONSE": {
            "authenticationFlag": true,
            "sessionId": "3C4AA754D77BFBE33E0D66EBE306B8CA",
            "statusMessage": "Successful Login.",
            "locId": 1,
            "userName": "Joe Schmoe"
        }
    }
}

我在使用objectForKey和valueForKey NSDictionary方法解析时遇到问题。我一直收到invalidArgumentException运行时错误。

I'm having problem parsing using the objectForKey and valueForKey NSDictionary methods. I keep getting invalidArgumentException runtime errors.

例如,我想查询authenticationFlag元素的响应数据。

For instance, I want to query the response data for the "authenticationFlag" element.

谢谢,
Mike
西雅图

Thanks, Mike Seattle

推荐答案

如果没有更多细节,很难说清楚(例如,您正在使用的JSON解析代码),但有两件事情让我感到不安:

It is hard to tell without some more details (e.g. the JSON parsing code that you are using), but two things strike me as possible:


  1. 您没有查询一条完整的道路。在上面的例子中,你需要首先获得封闭模型,json响应,然后才向json响应字典询问authenticationFlag值:

  1. you are not querying with a full path. In the case above, you'd need to first get the enclosing model, the json response, and only then ask the json response dictionary for the authenticationFlag value:

[[[jsonDict objectForKey:@model]
objectForKey:@JSONRESPONSE] objectForKey:@authenticationFlag]

也许你正在使用c-strings()而不是NSStrings( @)作为键(虽然这可能会崩溃或只是不编译)。密钥应该是可以转换为id的东西。

perhaps you're using c-strings ("") rather than NSStrings (@"") as keys (although this would likely crash nastily or just not compile). The key should be something than can be cast to id.

虽然可能,但两者都可能是假的,所以请包括更多细节。

While possible, both are probably false, so please include more detail.

这篇关于如何使用JSON框架和Objective-C / iPhone / Xcode解析嵌套的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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