【问题标题】:Download JSON data via NSJSONSerialization通过 NSJSONSerialization 下载 JSON 数据
【发布时间】:2013-01-30 16:24:21
【问题描述】:

我有一个来自 PHP 的 json_encode() 的 JSON 字符串。这是它在JSONViewer.stack.hu 中的样子,也是它在browser 中的样子。

是否可以使用 NSJSONSerialization 直接下载 JSON 数据?如果是这样,我将使用FMDB 将下载的 JSON 数据保存到 SQLite。

【问题讨论】:

    标签: iphone json sqlite fmdb nsjsonserialization


    【解决方案1】:
    id jsonObjectFromUrlString(NSString *urlString)
    {
        NSURL *url = [NSURL URLWithString:urlString];
        NSError *error = nil;
        id jsonObject = nil;
        NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
        if(error)
            NSLog(@"%@", error);
        else
            jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        if(error)
            NSLog(@"%@", error);
        return jsonObject;
    }
    

    【讨论】:

    • 它可以下载网站中的 JSON 数据吗?我还没有测试过。
    • 你为什么不试试并打印出结果呢?
    • 我可以使用 NSLog 输出它:( { article = "This is a dummy article about Apple Releases iPad 3"; "date_string" = "1969-12-31 19:00:05"; id = 1; timestamp = 5; title = "Apple Releases iPad 3"; }, {...;}, {...;} )
    【解决方案2】:

    JSON 序列化方法。请参考这个。 JSON Serialization

    对于数据库,您是否查看过 coredata. 请看看这些。

    add core data to existing xcode project.

    A simple intro to use coreData

    【讨论】:

    • 是的,我研究了 Core Data。不幸的是,我还没有完全掌握它的想法,所以我暂时坚持使用 FMDB。
    • 问题很简单,当您使用restkit或类似的东西时,您可以直接创建模型并减少应用程序正确运行的处理时间。
    • 不...是 id。如果你得到的数据是一个数组,它采用 NSArray 的形式,如果你只得到一个对象,它采用 NSDictionary 的形式。完全取决于出现。
    • 是的,它采用了 NSArray 的形式。那么,我是否下载了 JSON 数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多