- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我知道有很多问题都解决了这个问题,但我还没有找到对我有帮助的...
当我解析从本地主机(MAMP 服务器)下载的 json 数据时,我遇到 json 错误 3840,指出字符 0 周围的值无效...
我不明白为什么,因为我的 php 脚本在我的数组上显示了一个 var_dump(数组的数组):
array(2) { [0]=> array(5) { ["ID"]=> string(1) "1" ["EDS"]=> string(4) "1000" ["lastname"]=> string(8) "My lastname" ["firstname"]=> string(9) "My firstname" ["dateOfBirth"]=> string(10) "19.12.1975" } [1]=> array(5) { ["ID"]=> string(1) "2" ["EDS"]=> string(4) "1001" ["lastname"]=> string(14) "Smith" ["firstname"]=> string(6) "John" ["dateOfBirth"]=> string(10) "11.11.1111" } }
...这对我来说似乎是一个有效的 json 数组。
当我记录下载的 NSMutableData 时,它不是空的,而是类似于
76353648 2734b0a9 (+ around fifty like this).
不知道是不是因为数据不全,不知道怎么继续往下分析到底哪里出了问题。
如果有人知道会发生什么(我知道这与无法识别的特殊字符有关),那就太好了。
非常感谢!
编辑:为原始问题添加后续代码:
在
(void)connectionDidFinishLoading:(NSURLConnection *)connection {
id jsonObject = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSArray *deserializedArray = (NSArray *)jsonObject;
for (NSDictionary *jsonElement in deserializedArray)
{
Person *newPersonElement = [[PersonStore sharedStore] createPerson]; // --> what makes the app crash. But this method is working everywhere else...
// more code omitted
}
我不知道为什么这个初始化会在这里崩溃......
最佳答案
更新的答案:
我认为既然您已经发布了使用此接收到的 JSON 的 Objective-C 代码,那么实际问题是什么就变得很清楚了。
在我看来,您正在使用 Core Data (PersonStore
) 来保存传入的数据。
如果您从调用 connectionDidFinishLoading:
的同一线程调用 Core Data,您很可能会遇到线程问题,Core Data 不高兴您从主线程以外的线程。
试一试:在您的 connectionDidFinishLoading:
中将您的代码包装在以下内容中:
dispatch_async(dispatch_get_main_queue(), ^{
// Include the code here that walks over the incoming JSON
// and creates new `Person` instances.
});
这将在主线程上执行该 block 中的所有内容。这通常是核心数据使用的好主意。 (甚至可能需要,查看文档,如果我没记错的话,有一个关于核心数据和线程的特殊部分)。
很好奇这是否有效。
旧答案:
vardump
的输出实际上不是有效的 JSON。您需要改用 json_encode()
函数。
您可以通过执行以下操作将从服务器接收到的 NSData
转换为字符串:
if let s = String(data: data, encoding: NSUTF8StringEncoding) {
println(s)
}
您没有提到您是在 Swift 还是 Objective-C 中工作,但以上内容很容易转换为 Objective-C。
关于php - json 解析失败 - cocoa 错误 3840 和字符 0 周围的无效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28909723/
我是 Mac OS X 开发新手。在 XCode/Cocoa 开发环境中,每个 Objective-C 项目都以 开始 #import 它工作正常,但我对 Cocoa.h 文件位置感到困惑。我的文件
所以我开始阅读这本书: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022 第 2 章解释了 MVC 设计模式,并给
我使用下面的代码来访问项目中的变量 appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; UIApplic
我想从我的表格 View 中拖动一行并将其放入 Mac OS X 10.6 中的任何其他 NSTextField 中,并放置一串文本。 拖放已经在我的应用程序中工作(在 NSTableView 和 N
如何在另一个窗口中加载 Nib ? 我尝试了 initWithWindowName, if (mmController == NULL) mmController = [[mainMenu a
其中一个类使用#import 。所以,在我的Podspec ,我包括 framework Cocoa 我的Podspec是 Pod::Spec.new do |s| s.name
有没有可以让我创建简单的条形图和折线图的框架? 最佳答案 有更新的开源Core Plot。 关于cocoa - cocoa :创建图表,我们在Stack Overflow上找到一个类似的问题: htt
如何从 SIMBL 插件获取主应用程序中的单例?当我尝试调用诸如 [ProcessControl sharedInstance] 之类的内容时,我收到一条错误,指出 ProcessControl 未定
我正在尝试从我的 cocoa 应用程序(通过使用 Mail.app)发送电子邮件中的一些文本。最初我尝试使用 HTML 发送格式正确的文本。但 mailto: URL 不支持 html 标签(即使在设
我正在创建一个应用程序,该应用程序必须与服务器数据交互,然后相应地显示数据库中的结果。我正在用 Cocoa 编写客户端应用程序。 示例:用户登录到 Web 应用程序。他们有一些提交网络报告的选项。选项
我想创建一个可以在多个应用程序中使用的框架(如 coreData、CoreAudio 等)。 任何人都可以发布此链接或教程... 最佳答案 尝试苹果的 Framework Programming Gu
我正在使用 [[NSFontManager sharedFontManager] collectionNames] 获取所有集合,但我看到一些未翻译的字符串(例如“com.apple.AllFonts
我刚刚开始我的 cocoa 教育,我有一个简单的问题,我看到单击一个单词并使用 mac 文本转语音功能的能力表明文本是自动内置的。 (即 - 对于 hello world 应用程序,您可以单击 hel
我需要在加密中实现盐,但要做到这一点,我需要将其存储为我需要创建的文件格式,以便稍后检索它进行解密。在加密方面我是个菜鸟。文件格式规范如下: 密文:密文长度;salt:盐的长度; 然后将密文和盐写出来
有没有办法在Cocoa中以任意的能力创建贝塞尔路径?例如,对于我的应用程序的一部分,我需要一个起伏的单元格。因此,我想使用 10 到 50 个不同的点绘制一条曲线,形成一个循环。这些点将随机波动。我认
我想将声音存储在用户默认值中。既然我们不能直接存储语音,那么存储它的最佳方式是什么?安装新语音后,使用数组 [NSSpeechSynthesizer availableVoices] 中的索引可能会有
我一直在寻找解决方案,但不知道是否可以执行以下操作: 我有一个drawRect方法,我想要做的是将图形元素(例如矩形和线条)添加到当前 View 而不刷新它。我曾经调用 setNeedsDisplay
美好的一天! 我正在为 Mac OS X(不是 iPhone)开发提醒软件。它应该一天一次显示一个窗口。具体时间没有规定,只是一次。我怎样才能做到呢。它会自行注册以在登录后启动。我已经尝试过 NSTi
我只是想知道是否可以创建具有层次结构的下拉或弹出菜单?我目前正在开发的应用程序跟踪作业、类(class)和主题。当用户创建作业时,他们需要能够从下拉列表中选择它所属的类(class),但我也不希望通过
是否有任何 Cocoa Widget 可以用来构建典型的(除了 Interface Builder 中的)GUI 构建器属性检查器,例如 RealBasic 或 Delphi? 是否有一个网站列出了其
我是一名优秀的程序员,十分优秀!