AFNetworking-JSON:接收“数字”代替JSON [英] AFNetworking - JSON: Receiving "numbers" instead of JSON

查看:112
本文介绍了AFNetworking-JSON:接收“数字”代替JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了AFNetworking HTTP Client子类,并将HTTP操作注册为AFJSONRequestOperation。
并设置默认标头,如下所示:

  [self setDefaultHeader:@ Content-Type value:@ application / json]; 
[self setDefaultHeader:@ Accept值:@ text / html];

然后我像这样调用RESTful服务器:

  [[TheAPIClient sharedClient] getPath:domain参数:无成功:^(AFHTTPRequestOperation * operation,id JSON){

NSLog(@ %@,JSON);





}故障:^(AFHTTPRequestOperation * operation,NSError * error){

NSLog(@失败);


}];

但是我得到的只是很多数字,例如:



'pre> < 7b0d0a20 20224d65 6e755365 74223a20 7b0d0a20 20202022 4b617465 676f7269 53657422 3a205b0d 0a202020 2020207b 0d0a2020 20202020 20202245 6c656d65 6e745365 74223a20 5b0d0a20 20202020 20202020 207b0d0a 20202020 20202020 20202020 22496422 3a20312c 0d0a2020 20202020 20202020 2020224e 61766e22 3a202252 656a6520 636f636b 7461696c 222c0d0a 20202020 20202020 20202020 22426573 6b726976 656c7365 223a2022 52656a65 723f222c 0d0a2020 20202020 20202020 20202250 72697322 3a203134 392c0d0a 20202020 20202020 20202020 224b6174 65676f72 69536574 4964223a 20310d0a 20202020 20202020 20207d0d 0a202020 20202020 205d2c0d 0a202020 20202020 20224964 223a2031 2c0d0a20 20202020 20202022 4e61766e 223a2022 466f7272 6574222c 0d0a2020 20202020 20202245 6c656d65 6e745365 74496422 3a20312c 0d0a2020 20202020 2020224d 656e755f 4964223a>

如何解决此问题?任何想法为什么会这样? :)

解决方案

JSON NSData 包含对象

  {
MenuSet:{
KategoriSet :[[
{
ElementSet:[
{
Id:1,
Navn: Reje cocktail,
Beskrivelse : Rejer?,
Pris:149,
KategoriSetId:1
}
....

您可以使用


$ b $将其转换为 NSDictionary b

  NSError *错误; 
NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:& error];


I have set up an AFNetworking HTTP Client subclass and registered the HTTP operation as AFJSONRequestOperation. And set default headers like so:

[self setDefaultHeader:@"Content-Type" value:@"application/json"];
    [self setDefaultHeader:@"Accept" value:@"text/html"];

Then I am making a call to a RESTful server like so:

     [[TheAPIClient sharedClient] getPath:domain parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {

        NSLog(@"%@", JSON);





    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"FAILED"); 


    }];

But all I getting is a lot of numbers like so:

<7b0d0a20 20224d65 6e755365 74223a20 7b0d0a20 20202022 4b617465 676f7269 53657422 3a205b0d 0a202020 2020207b 0d0a2020 20202020 20202245 6c656d65 6e745365 74223a20 5b0d0a20 20202020 20202020 207b0d0a 20202020 20202020 20202020 22496422 3a20312c 0d0a2020 20202020 20202020 2020224e 61766e22 3a202252 656a6520 636f636b 7461696c 222c0d0a 20202020 20202020 20202020 22426573 6b726976 656c7365 223a2022 52656a65 723f222c 0d0a2020 20202020 20202020 20202250 72697322 3a203134 392c0d0a 20202020 20202020 20202020 224b6174 65676f72 69536574 4964223a 20310d0a 20202020 20202020 20207d0d 0a202020 20202020 205d2c0d 0a202020 20202020 20224964 223a2031 2c0d0a20 20202020 20202022 4e61766e 223a2022 466f7272 6574222c 0d0a2020 20202020 20202245 6c656d65 6e745365 74496422 3a20312c 0d0a2020 20202020 2020224d 656e755f 4964223a >

How can I solve this problem? any ideas why this is happening? :)

解决方案

JSON is a NSData object containing

{
  "MenuSet": {
    "KategoriSet": [
      {
        "ElementSet": [
          {
            "Id": 1,
            "Navn": "Reje cocktail",
            "Beskrivelse": "Rejer?",
            "Pris": 149,
            "KategoriSetId": 1
          }
....

You can convert this to a NSDictionary with

NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:&error];

这篇关于AFNetworking-JSON:接收“数字”代替JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆