我使用NSJSONSerialization's JSONObjectWithData:data options: error:来解析从服务器返回的JSON数据.
现在我使用的选项参数:NSJSONReadingAllowFragments.您可以在下面查看实际的JSON(我认为问题出在哪里).
我得到的错误信息是:
错误域= NSCocoaErrorDomain代码= 3840"操作无法完成.(Cocoa错误3840.)"(字符0周围的值无效.)UserInfo = 0x6895da0 {NSDebugDescription =字符0周围的值无效}
知道怎么解决吗?
JSON =
{"name":"Johan Appleseed",
"email":"j.appleseed@emuze.co",
"phone":"+4121876003",
"accounts":{
"facebook":[true,1125],
"twitter":[false,null],
"homepage":[true,"http:\/\/johnAplleseed.com\/index.html"]}}
Run Code Online (Sandbox Code Playgroud) 我正在使用NSJSONSerialization将json字符串转换为NSDictionaray
JSON字符串是
{"bid":88.667,"ask":88.704}
Run Code Online (Sandbox Code Playgroud)
在NSJSONSerialization之后
{
ask = "88.70399999999999";
bid = "88.667";
}
Run Code Online (Sandbox Code Playgroud)
有人知道这个问题吗?
我想将NSDictionary转换为json string.everything工作正常,我有一个小问题,描述如下:我有一个以下代码将NSDictionary转换为NSString:
-(NSString *)dictToJson:(NSDictionary *)dict
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
return [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
}
Run Code Online (Sandbox Code Playgroud)
我将该方法称为:
NSLog(@"%@", [self dictToJson:@{@"hello" : @"21/11/2014 10:07:42 AM"}]);
Run Code Online (Sandbox Code Playgroud)
以下是此NSLog的输出:
{
"hello" : "21\/11\/2014 10:07:42 AM"
}
Run Code Online (Sandbox Code Playgroud)
我期待以下输出,我怎样才能实现它:
{
"hello" : "21/11/2014 10:07:42 AM"
}
Run Code Online (Sandbox Code Playgroud)
它可以通过使用stringByReplacingOccurrencesOfString方法来完成,但我不想使用它.还有其他方法可以达到同样的目的吗?
当我将数据从Web服务转换为NSDictionary时,我陷入困境.但是当在调试模式下访问控制台时,它会返回,而当我将字典的值与视图绑定时,它可以完美地工作.以下是代码: -
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&e];
Run Code Online (Sandbox Code Playgroud)
在控制台我使用po [dictName valueForKey:@"Status"]以及po [dictName objectForKey:@"Status"].遵循几个步骤,但对我不起作用1.在此模式下将优化级别设置为none.2.在此模式下已编程调试模式.
目前我正在编写一个应用程序(目标iOS 6,启用了ARC),它使用JSON进行数据传输,使用Core Data进行持久存储.JSON数据由PHP脚本通过json_encode从MySQL数据库生成.
我的问题是,对于某些表中的数据,以下代码失败:
- (NSDictionary *)executeFetch:(NSString *)query
{
NSURL *requesturl = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *dataError = nil;
self.jsonData = [NSData dataWithContentsOfURL:requesturl options:kNilOptions error:&dataError];
NSError *error = nil;
self.jsonSerializationResult = [NSJSONSerialization JSONObjectWithData:self.jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];
return self.jsonSerializationResult;
}
Run Code Online (Sandbox Code Playgroud)
该程序总是在EXC_BAD_ACCESS错误的位置崩溃,它表示self.jsonSerializationResult和Instruments说有一个Zombie被检测到.我知道这意味着我发送消息的一些对象是零,但我无法找到如何解决它...这就是乐器所说的:
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0xa1b8a70 CFString (mutable) Malloc 1 00:01.603.081 32 Foundation -[NSPlaceholderMutableString initWithBytesNoCopy:length:encoding:freeWhenDone:]
1 0xa1b8a70 CFString (mutable) Release 0 00:01.603.137 0 Foundation newJSONValue
2 0xa1b8a70 CFString (mutable) Zombie -1 00:01.603.259 0 …Run Code Online (Sandbox Code Playgroud) 我试图解析一个ios 6应用程序的JSON,但似乎无法让它工作.我已经搜索了大量的论坛,但没有找到一个有效的解决方案,我理解足以实现,或者适用.
如果有一个我错过了,我道歉.
首先,我有一个测试WebService,据我所知,返回有效的JSON
http://thetrouthunter.com/SVLocationsAPI.php
其次,这是我的Objective-C代码:
+ (NSDictionary *)connectToService:(NSString *)query
{
NSError *error = nil;
query = [NSString stringWithFormat:@"%@&format=json&nojsoncallback=1", query];
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
NSLog(@"locations: %@", results);
if (error)
NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
return results;
}
+ (NSArray *)userLocation {
NSString *request = [NSString stringWithFormat:@"http://thetrouthunter.com/SVLocationsAPI.php"];
return [[self connectToService:request] valueForKeyPath:@"locations.location"];
}
Run Code Online (Sandbox Code Playgroud)
ls NSLog函数打印出错误:"操作无法完成.(可可错误:3840.)"
我无法弄清楚为什么会这样.我尝试过各种各样的事情.
使用Xcode 5.0升级到UrbanAirship 3.0.0,调用此代码时收到错误:
[UAirship takeOff:config];
Run Code Online (Sandbox Code Playgroud)
错误是
+[NSJSONSerialization stringWithObject:]: unrecognized selector sent to class 0x3b2ca9fc
2013-09-19 15:02:31.981 [178:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSJSONSerialization stringWithObject:]: unrecognized selector sent to class 0x3b2ca9fc'
Run Code Online (Sandbox Code Playgroud)
当将"inProduction"键设置为"YES"时似乎没有出现这种情况AirshipConfig.plist,这有意义,因为调用takeOff:填充UAirShip的实例.这似乎是由于类别NSJSONSerialization+UAAdditions.
对此有何帮助?谢谢
我从我的服务器上获取了一个有效的响应:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
//[_responseData appendData:data];
NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseBody);
if(data != NULL)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
NSError *error = nil;
//NSMutableArray *jsonArray = [[CJSONDeserializer deserializer] deserializeAsArray:[responseBody dataUsingEncoding:NSUTF8StringEncoding] error:&error];
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (error)
{
NSLog(@"JSONObjectWithData error: %@", error);
[delegate onErrorGetArrayFromServer];
}
else
[self parseJSON:jsonArray];
});
}
else
{
if([delegate respondsToSelector:@selector(onErrorGetArrayFromServer)])
{
[delegate onErrorGetArrayFromServer];
}
} …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)
ios ×6
json ×5
objective-c ×4
ios6 ×2
ios7 ×2
iphone ×1
nsdictionary ×1
parsing ×1
web-services ×1
xcode ×1
xcode8 ×1