【问题标题】:IPhone parsing JSON [duplicate]iPhone解析JSON [重复]
【发布时间】:2011-11-04 13:06:17
【问题描述】:

可能重复:
How to parse JSON in iOS App

我有以下 json:

NSString *jsonResult = @"{\"code\":\"000\",\"message\":\"success\",\"datas\":[{\"datas\":[{\"service_name\":\"user_info\",\"name\":\"Account Test 32\",\"number\":123,\"company\":\"ABC\",\"currency\":\"USD\",\"balance\":\"1000\",\"profit\":\"-80.00\",\"credit\":\"0.00\",\"equity\":\"1000.00\",\"leverage\":100,\"free_margin\":\"1000.00\",\"free_margin_mode\":1.0,\"server_name\":\"localhost\",\"stopout_level\":5,\"stopout_mode\":0}],\"service_name\":\"user_info\"}]}";

谁能举例说明如何解析这个?

【问题讨论】:

  • 你为什么要解析 JSON?那是您从网络服务获得的有效负载吗?
  • 是的,它是来自网络服务的负载。

标签: ios json


【解决方案1】:

试试这个。导入SBJson parser,你也可以使用TouchJson,但我更喜欢SBJson。

#import "JSON.h"

// Create SBJSON object to parse JSON
SBJSON *parser = [[SBJSON alloc] init];

// parse the JSON string into an object - assuming json_string is a NSString of JSON data
NSDictionary *object = [parser objectWithString:jsonResult error:nil];

【讨论】:

  • 如何获取数据数组中的值?
  • 你有你的jsonResult 对吗?将其传递给 JSON 解析器。它将转换为 ObjectiveC 对象...这就是您需要的对吗?所以我写的是解析器部分。在哪里导入 json 解析器并解析字符串....
  • 它仍然给我一个错误:SBJsonParser json = [[SBJsonParser alloc] init]; NSError *jsonError; NSDictionary *parsedJSON = [json objectWithString:jsonResult error:&jsonError]; NSDictionary jsonValue = [parsedJSON objectForKey:@"code"]; NSLog(@"code: %@", [jsonValue objectForKey:@"code"]);
  • 所以它的工作?为什么要发布代码?
  • 不工作。抱歉发布代码。
【解决方案2】:

您最好的选择是使用 JSON 解析库,例如 TouchJson

【讨论】:

    猜你喜欢
    • 2012-03-09
    • 2011-08-26
    • 1970-01-01
    • 2012-04-10
    • 2016-02-24
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多