我正在使用TWrequest在桌面视图中显示我的推特列表.以下代码有效.问题是更新表格非常慢.我在NSlogging请求响应(发生得非常快),我也循环遍历每个列表并将列表'name'添加到一个数组(再次,很快发生<1s).但由于一些莫名其妙的原因,该表大约需要4秒左右才能更新.
为什么表重新加载需要这么长时间?问题是没有解析响应(因为我可以看到nslog这很快发生),它需要很长时间才能显示在表中?非常感谢帮助!
-(IBAction)getLists{
// First, we need to obtain the account instance for the user's Twitter account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your request
NSLog(@"User rejected access to the account.");
}
else {
// Grab the available accounts
twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) { …Run Code Online (Sandbox Code Playgroud) 我将JSON映射到基础对象时遇到问题...
当JSON字符串不包含该键时,有没有办法将"nil"值放到类成员变量上?
例如,这是我的DataModel类:
@interface DataModel : JSONModel
@property (strong, nonatomic) NSString* Status;
@property (strong, nonatomic) NSString* ErrorCode;
@property (strong, nonatomic) ClientModel* Client;
@end
Run Code Online (Sandbox Code Playgroud)
该类用于解析ClientModel对象中有关客户端的一些信息,并具有ErrorCode和Status字段.
状态字段可以包含"确定"或"错误".这里没问题.如果json包含"Error"作为状态,则下一个字段是ErrorCode.
问题是服务器不会总是向我发送ErrorCode的键值(当状态为"Ok"时,服务器API不发送ErrorCode键值).
考虑到我无法修改服务器发送的JSON,如何解析我的模态对象中收到的JSON字符串,并在JSON字符串不包含时将"nil"作为ErrorCode值?
有没有允许我们这样做的图书馆?我目前正在使用JSONModel,但是当缺少密钥时,它给出了"传入数据无效"错误.我知道用于java的Jackson API处理这种情况,但我不知道在Objective-C中是否有类似的东西.如果我可以使用NSJSONSerialization进行解决方法,我也很高兴听到它.
谢谢!
我试图在iOS中解析mapquest地理编码json,但NSJSONSterilization返回null.我检查了在线json检查器,看起来网址实际上是一个json.
这是代码 NSJSONSerialization
if(geocodeResponseData)
{
NSLog(@"there is response data");
//this is logged.
}
NSDictionary *mapQuestReponse = [NSJSONSerialization
JSONObjectWithData:geocodeResponseData
options:kNilOptions
error:&error];
NSLog(@"mapquestreponse %@", mapQuestReponse);
Run Code Online (Sandbox Code Playgroud)
这是JSON为解析而返回的URL.
http://www.mapquestapi.com/geocoding/v1/batch?key=API----KEY----HIDDEN----&callback=renderBatch&outFormat=json&location=14443%20C%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14510%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14550%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14515%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1480%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095129&location=1600%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095129&location=18486%20Prospect%20Rd,San%20Jose,%20CA%2095070&location=14572%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=5210%20Prospect%20Rd,San%20Jose,%20CA%2095129&location=1600%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095106&location=14480%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1818%20Saratoga%20Ave,San%20Jose,%20CA%2095129&location=18562%20Prospect%20Rd,Saratoga,%20CA%2095070&location=14560%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14420%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1075%20S%20De%20Anza%20Blvd,Cupertino,%20CA%2095129&location=18802%20Cox%20Ave,Saratoga,%20CA%2095070&location=6154%20Bollinger%20Rd,San%20Jose,%20CA%2095129&location=14555%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14000%20Fruitvale%20Ave,Saratoga,%20CA%2095070
Run Code Online (Sandbox Code Playgroud)
是因为回来NULL了utf-8吗?提前致谢
我一直在寻找是否有可能获得一个JSON字典或数组,并直接将它映射到一个自定义对象,其属性与JSON标签的名称相同,但我找不到任何有关它的信息.
我一直在手动解析JSON字典,如下所示:
id deserializedObj = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error];
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
if ([jsonDictionary objectForKey:idTag] != [NSNull null])
[myObject setID:[[jsonDictionary objectForKey:@"id"] integerValue]];
// Rest of properties
}
Run Code Online (Sandbox Code Playgroud)
但我觉得奇怪的是必须手动解析每个字典条目而没有办法直接将其序列化为自定义对象,是不是还有其他更快的方法?
注意:我需要我的应用程序与iOS 5+兼容
提前致谢
我正在使用最新的SDK开发iOS 5及更高版本的应用程序.
我必须解析这个JSON:
{"GetHoroscope":false,"GetQuoteOfTheDay":false, ... }
Run Code Online (Sandbox Code Playgroud)
要做到这一点,我有这个代码:
- (NSDictionary*)getDictionaryFromNSData:(NSData*)jsonData
{
NSError* error = nil;
id jsonObject = [NSJSONSerialization
JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error];
if ((jsonObject != nil) && (error == nil))
{
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSDictionary* deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
return deserializedDictionary;
}
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
但我对boolean价值观有疑问.当我检查时,deserializedDictionary我看到 GetHoroscope并且GetQuoteOfTheDay值为空.
我是否需要使用布尔值做一些特殊的事情?
我正在使用FHSTwitterEngine将gif发布到twitpic.当我在iphone上有wifi或3G连接时,一切正常.但是我也希望在没有连接或上传失败时实现一些错误处理.因此,为了测试我将iphone置于飞行模式并尝试使用以下方法上传到twitpic:
id returned = [[FHSTwitterEngine sharedEngine] uploadImageToTwitPic:gif
withMessage:@"message" twitPicAPIKey:@"key"];
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我立即得到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'data parameter is nil'
Run Code Online (Sandbox Code Playgroud)
然后xcode指向FHSTwitterEngine框架中的这行代码:
id parsedJSONResponse = removeNull([NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers error:nil]);
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题的任何想法?
objective-c twitpic ios nsjsonserialization fhs-twitter-engine
我知道有类似的问题发布,因为我已经阅读了大多数所有这些并且仍然遇到问题.我正在尝试将JSON数据发送到我的服务器,但我不认为正在接收JSON数据.我只是不确定我错过了什么.以下是我的代码......
将数据发送到服务器的方法.
- (void)saveTrackToCloud
{
NSData *jsonData = [self.track jsonTrackDataForUploadingToCloud]; // Method shown below.
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString); // To verify the jsonString.
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:http://www.myDomain.com/myscript.php] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[postRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[postRequest setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[postRequest setHTTPBody:jsonData];
NSURLResponse *response = nil;
NSError *requestError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&requestError];
if (requestError == nil) {
NSString *returnString = [[NSString …Run Code Online (Sandbox Code Playgroud) 我是Objective-C的新手,我是一名核心的Java和Python老手.
我创建了一个Objective-C脚本来调用URL并获取URL返回的JSON对象:
// Prepare the link that is going to be used on the GET request
NSURL * url = [[NSURL alloc] initWithString:@"http://domfa.de/google_nice/-122x1561692/37x4451198/"];
// Prepare the request object
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Prepare the variables for the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
// Construct a Array around the Data from the response
NSArray* object = [NSJSONSerialization
JSONObjectWithData:urlData
options:0
error:&error];
//NSLog(object);
// Iterate through …Run Code Online (Sandbox Code Playgroud) 这是我获取JSON的代码,它与我在其他问题上找到的这个网址一起工作:http://binaenaleyh.net/dusor/.但是,当我使用它与这个网址:http://www.netcampus.fr/api/schools,它根本不起作用.我有一个错误说:"exc_breakpoint(code = exc_i386_bpt subcode = 0x0)"
我的代码是错误的,还是JSON数据?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var myData:NSData = getJSON("http://www.netcampus.fr/api/schools")
println(myData) // show me data
var myDict:NSDictionary = parseJSON(myData)
println(myDict)
}
func getJSON(urlToRequest: String) -> NSData{
return NSData(contentsOfURL: NSURL(string: urlToRequest))
}
func parseJSON(inputData: NSData) -> NSDictionary{
var error: NSError?
var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary // error here
return boardsDictionary
}
}
Run Code Online (Sandbox Code Playgroud) 我使用以下行将a转换NSDictionary为JSON NSData:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:answers
options:NSJSONWritingPrettyPrinted
error:&err];
Run Code Online (Sandbox Code Playgroud)
并将其传递给服务器端,这是一个PHP脚本.该脚本将JSON字符串读取为:
{
"A" : "1941",
"D" : "1699",
"B" : "1949",
"E" : "1823",
"C" : "1999"
}
Run Code Online (Sandbox Code Playgroud)
如何将JSON字符串格式化为1行,如下所示?
{"A" : "1941", "D" : "1699", "B" : "1949", "E" : "1823", "C" : "1999"}
Run Code Online (Sandbox Code Playgroud)
除了以外还有其他选择NSJSONWritingPrettyPrinted吗?