JSON缺少双引号iOS

时间:2014-10-09 01:58:08

标签: objective-c json xcode afnetworking-2

我遇到了一个奇怪的问题。我正在从我们的服务器解析json数据。当我通过浏览器访问它时,它会显示以下数据:

productCombinationAttributes": [
    {
      "value": "Green",
      "price": "0",
      "img_id": "1005",
      "name": "color",
      "id": "b_9"
    },
    {
      "value": "Blue",
      "price": "0",
      "img_id": "1005",
      "name": "color",
      "id": "b_10"
    },
    {
      "value": "Yellow",
      "price": "0",
      "img_id": "1005",
      "name": "color",
      "id": "b_14"
    },
    {
      "value": "Xl",
      "price": "100",
      "img_id": "1005",
      "name": "size",
      "id": "b_11"
    },
    {
      "value": "L",
      "price": "50",
      "img_id": "1005",
      "name": "size",
      "id": "b_12"
    },
    {
      "value": "Metal",
      "price": "0.00",
      "img_id": "1005",
      "name": "material",
      "id": "b_13"
    }
  ],
  "productSpecification": [],
  "paymentMethod": [
    "Credit or Debit Card",
    "Paypal",
    "Dragon Pay",
    "Cash on Delivery"
  ],
  "productCombinatiobDetails": [
    {
      "quantity": "12",
      "combinationId": [
        "b_9",
        "b_12",
        "b_13"
      ],
      "id": 1519,
      "location": {
        "NCR": "100.0000",
        "Makati": "120.0000"
      }
    },
    {
      "quantity": "12",
      "combinationId": [
        "b_14",
        "b_11",
        "b_13"
      ],
      "id": 1520,
      "location": {
        "NCR": "100.0000",
        "Makati": "120.0000"
      }
    }
  ]

但是当我通过XCODE访问它并使用afnetworking或其他解析方法解析它。 JSON看起来像这样:

productCombinationAttributes =     (
                {
            id = "b_9";
            "img_id" = 1005;
            name = color;
            price = 0;
            value = Green;
        },
                {
            id = "b_10";
            "img_id" = 1005;
            name = color;
            price = 0;
            value = Blue;
        },
                {
            id = "b_14";
            "img_id" = 1005;
            name = color;
            price = 0;
            value = Yellow;
        },
                {
            id = "b_11";
            "img_id" = 1005;
            name = size;
            price = 100;
            value = Xl;
        },
                {
            id = "b_12";
            "img_id" = 1005;
            name = size;
            price = 50;
            value = L;
        },
                {
            id = "b_13";
            "img_id" = 1005;
            name = material;
            price = "0.00";
            value = Metal;
        }
    );

这是解析它的代码:

   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"url.com"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        [self jsonParser:parentView scrollView:sv_parent];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

问题是我在请求后显示返回数据后双引号丢失。有人可以帮我弄这个吗?提前谢谢你:)

1 个答案:

答案 0 :(得分:1)

那不缺。您的打印是[NSDictionary description]而不是json字符串。 AFHTTPRequestOperationManager已经为你解析了json到NSDictionary。 如果要获取原始json字符串,可以使用[NSJSONSerialization dataWithJSONObject:options:error:]将NSDictionary序列化为json字符串。

NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:0 error:nil];
NSString *jsonString = [NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];