【问题标题】:how to use json_decode in Objective-C如何在 Objective-C 中使用 json_decode
【发布时间】:2015-02-17 15:38:28
【问题描述】:

我从 Rest API 请求数据,它的数据在响应客户端之前被编码。我的响应对象是

{homes = "[{\"info\":{\"id\":\"001\",\"description\":\"\",\"posted_date\":\"2015-02-12 09:42:31\"}},{\"info\":{\"id\":\"002\",\"description\":\"test\",\"posted_date\":\"2015-02-12 09:42:31\"}}]";}

我想使用 Objective-c 来解码或将此对象转换为这种格式:

{ 
   homes = (
       { info = { id = 001; "description":""; "posted_date" = "2015-02-12 09:42:31";}};
       { info = { id = 002; "description":"test"; "posted_date" = "2015-02-12 09:42:31";}};
      );
}

请给我一些想法。

提前致谢

【问题讨论】:

  • 请做一些基本的搜索。在此处或通过 Google 进行快速搜索会发现用于在 Objective-C 中解析 JSON 数据的资源数量之多。
  • 现在是 asurd_number += 3

标签: objective-c json


【解决方案1】:

你应该使用[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];这样的方法

data参数是NSData对象,可以通过调用[string dataUsingEncoding:NSUTF8StringEncoding]来求值

【讨论】:

    【解决方案2】:

    我通过使用解决了我的问题

    SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; NSMutableArray *tmpHome = [jsonParser objectWithString:[JSON valueForKey:@"homes"] error:nil];

    非常感谢所有好主意:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2011-02-10
      • 1970-01-01
      • 2011-07-07
      相关资源
      最近更新 更多