我有一些公共json服务的问题,服务器格式化这种方式
jsonFlickrFeed({
"title": "Uploads from everyone",
"link": "http://www.flickr.com/photos/",
"description": "",
"modifi ... })
Run Code Online (Sandbox Code Playgroud)
NSJSONSerialization似乎无法开展工作
NSURL *jsonUrl = [NSURL URLWithString:@"http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback"];
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl options:kNilOptions error:&error];
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", jsonResponse);
Run Code Online (Sandbox Code Playgroud) 我有一本字典,当我记录它时显示...
{
Date = "2013-04-30 17:17:18 +0000";
Description = Kb;
EventID = "92193e58-c04a-4233-9a6c-1332bc056b20";
Title = Keyboard;
}
Run Code Online (Sandbox Code Playgroud)
我正试图把它变成NSData,用于像这样的JSON Web服务......
- (NSData *)JSONRepresentation
{
NSDictionary *dictionary = [self dictionaryObject];
NSError *jsonError;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:dictionary
options:0
error:&jsonError]; //This is where the error occurs.
return JSONData;
}
Run Code Online (Sandbox Code Playgroud)
但每次我运行它时,应用程序都会崩溃.
字典正确形成,应用程序只是崩溃在这一行.
在AppCode中,我收到崩溃报告......
EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe))
Run Code Online (Sandbox Code Playgroud)
在Xcode中,应用程序停止,如果我尝试继续,它会因错误而停止...
EXC_BAD_ACCESS (code=1, address=0x0)
Run Code Online (Sandbox Code Playgroud) 我在用
imageData = UIImagePNGRepresentation(imgvw.image);
并在发布时
[dic setObject:imagedata forKey:@"image"];
Run Code Online (Sandbox Code Playgroud)
后
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&theError];
现在应用程序由于未捕获的异常而崩溃终止应用程序' NSInvalidArgumentException',原因:'JSON写入中的无效类型(NSConcreteMutableData)
我正在尝试创建一个简单的 JSON 对象,但仍然出现错误,我知道我的代码有什么问题:
NSString *vCard = [BRContacts getContacts]; // this is just a string, could be nil
NSDictionary *JSONdic = nil;
if (vCard)
{
JSONdic = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"status",vCard,@"data", nil];
}
else
{
JSONdic = [NSDictionary dictionaryWithObjectsAndKeys:@"0",@"status",@"vCard is empty",@"error", nil];
}
NSError *error = nil;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:JSONdic options:NSJSONWritingPrettyPrinted error:&error];
return [GCDWebServerDataResponse responseWithJSONObject:JSONdata];
Run Code Online (Sandbox Code Playgroud)
例外是
JSON 写入中的顶级类型无效
我也检查过JSONdic,在每种情况下都不是零。有什么建议?
我一直在乱砍几个小时试图解决这个问题无济于事 - 似乎我唯一的选择是在这里发帖,看看是否有人可以对这个问题有所了解.它可能是AFNetworking的问题,或者(更有可能),它可能是我的代码的问题.
我正在使用的代码完全适用于我的应用程序中99%的操作.我正在开发一个通过构建JSON搜索,发送它们并从服务器接收响应来利用Elastic Search的应用程序.
以下是返回的JSON示例:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "asx",
"_type": "61a88d3848b00655d9aa59db70847318",
"_id": "b91f9257744fedb4ef1c127e275c127c",
"_score": null,
"_source": {
"value": "22/06/1998"
},
"sort": [
4.439049394553e-312
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
现在,将其插入jsonlint.com(并了解一些关于JSON格式),很容易看出这是有效的JSON.
我正在使用AFHTTPClient子类来发布请求并接收数据.这是我用来POST的代码:
[super postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
NSData *responseData = [(AFJSONRequestOperation *)operation responseData];
NSDictionary *responseDictionary;
if (responseData != nil) {
responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL];
} …Run Code Online (Sandbox Code Playgroud) 我正在运行一个后台NSURLSession会话,我试图找出一种方法来获取NSURLDownloadTaskDelegate回调之一的JSON响应.我已将会话配置为接受JSON响应.
NSURLSessionConfiguration *backgroundSession = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.Att.Locker.BackgroundUpload"];
backgroundSession.HTTPAdditionalHeaders = @{ @"Accept":@"application/json"};
session = [NSURLSession sessionWithConfiguration:backgroundSession delegate:uploader delegateQueue:nil];
Run Code Online (Sandbox Code Playgroud)
我可以使用以下回调轻松解析NSURLSessionDownloadTasks的JSON响应.它以NSURL的形式将JSON响应写入沙箱.
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
//Reading the Json response from the sandbox using the NSURL parameter
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我遇到错误,上面的回调没有被调用,它似乎只有在成功下载的情况下被调用.由于我使用的是后台会话,因此我无法使用任何NSURLSessionDataDelegate回调.我只能使用NSURLSessionDownloadTaskDelegate和NSURLSessionTaskDelegate,而我可以使用以下回调获取任务响应.我没有在响应中看到JSON.
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)downloadTask.response;
NSDictionary *httpResponse = [response allHeaderFields];
NSLog(@"Response Header Fields:%@",[httpResponse allKeys]);
}
Run Code Online (Sandbox Code Playgroud)
NSURLConnection有一个didReceiveData参数,它为我们提供了一个NSData对象,我们可以用它来获取JSON响应.我没有在NSURLSession的委托回调中看到除了NSURLDataTask,但是我们不能在后台使用数据任务,那么我们应该如何获得JSON响应呢?任何帮助表示赞赏
编辑:
我通常在后台运行应用程序时遇到此问题(主要是当它被踢出内存而不是暂停时).我已经在appDelegate中实现了回调,并且我能够重新关联session.I认为didFinishDownloadingToURL仅在成功完成任务的情况下被调用,但是当任务失败时,不能保证它将被调用但是在每次出现故障时,都会调用didCompleteWithError
我JSON在我的代码中解析一个.但是在检索解析数据时我遇到了一些意想不到的问题JSON.那么让我解释一下我的问题.
我必须JSON使用xcode 解析以下数据.当我在浏览器中点击相同的URL时,这就是要解析的数据:
{
"RESPONSE":[
{"id":"20",
"username":"john",
"email":"abc@gmail.com",
"phone":"1234567890",
"location":"31.000,71.000"}],
"STATUS":"OK",
"MESSAGE":"Here will be message"
}
Run Code Online (Sandbox Code Playgroud)
我达到此JSON数据的代码如下:
NSData *data = [NSData dataWithContentsOfURL:finalurl];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
Run Code Online (Sandbox Code Playgroud)
如果我json使用打印对象NSLog,即NSLog(@"json: %@", [json description]);它看起来像:
json: {
MESSAGE = "Here will be message";
RESPONSE =(
{
email = "abc@gmail.com";
id = 20;
location = "31.000,71.000";
phone = 1234567890;
username = john;
}
);
STATUS = OK; …Run Code Online (Sandbox Code Playgroud) 我在页面上输出了一个JSON数组.我想将此JSON数组转换为NSArray.
这是网页上的JSON输出:
[{"city":"Current Location"},{"city":"Anaheim, CA"},{"city":"Los Angeles, CA"},{"city":"San
Diego, CA"},{"city":"San Francisco, CA"},{"city":"Indianapolis, IN"}]
Run Code Online (Sandbox Code Playgroud)
这是我的NSURL调用和NSJSONSerialization:
NSString *cityArrayURL = [NSString stringWithFormat:@"http://site/page.php";
NSData *cityData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cityArrayURL]];
NSError *error;
NSDictionary *cityJSON = [NSJSONSerialization JSONObjectWithData:cityData
options:kNilOptions error:&error];
Run Code Online (Sandbox Code Playgroud)
我想将名为cityJSON的NSDictionary转换为NSArray.有人能告诉我下一步可能是什么吗?谢谢!
试图让这个在Swift 2.0中工作,错误说:
类型NSJSONWritingOptions不符合协议NilLiteralConvertible
在var options = prettyPrinted...:
func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> String {
var options = prettyPrinted ? NSJSONWritingOptions.PrettyPrinted : nil
if NSJSONSerialization.isValidJSONObject(value) {
do{
let data = try NSJSONSerialization.dataWithJSONObject(value, options: options)
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
return string as String
}
} catch {
}
}
return ""
}
Run Code Online (Sandbox Code Playgroud) 我试图了解NSJSONSerialization类参考.由于缺少developer.apple.com网站上的代码示例,我输了.网上有数以百万计的例子和其他json库,但是我还没有能够使用最新版本的xcode.(我正在运行:版本4.3.1(4E1019)并在iPhone 5.0.1上进行测试)
我想使用按钮将json文件中的数据提取到我的iphone中.
让我说我从URL获取我的数据: http://companyurl/jsonfile.json(标准JSON格式)
jsonfile.json看起来像这样......;
{
"companylist":
[
{
"company":"Companyname 1",
"telephone":"1234567890",
"url":"http:\/\/www.companyname1.com\/",
"category":"category 1",
"position":"1",
},
{
"company":"Companyname 2",
"telephone":"2345678901",
"url":"http:\/\/www.companyname2.com\/",
"category":"category 2",
"position":"2",
},
{
"company":"Companyname 3",
"telephone":"3456789012",
"url":"http:\/\/www.companyname3.com\/",
"category":"category 3",
"position":"3",
}
]
}
Run Code Online (Sandbox Code Playgroud)
我在.h和我的.m文件中写什么?
谢谢你的帮助!:)
ios ×7
json ×6
objective-c ×6
iphone ×2
afnetworking ×1
cocoa ×1
gcdwebserver ×1
ios5 ×1
nsarray ×1
nsurlsession ×1
parsing ×1
swift2 ×1
xcode4.3 ×1