ios-转换后JSON响应获取空值

作者:用户 来源:互联网 浏览:350 次 时间:2016-06-14 09:50:21

iosjsoniphonensarray

ios-转换后JSON响应获取空值 - 摘要: 转换后JSON响应获取空值转换为NSArray后,JSOn响应得到的是空值JSONResponse:JSONLog:[{"0":"41","intid":"41","1":"u8a00u3046"

问题描述

转换后JSON响应获取空值

转换为NSArray后,JSOn响应得到的是空值

JSON Response :

JSON Log :

[
  {
    "0": "41",
    "intid": "41",
    "1": "u8a00u3046",
    "varfirstname": "u8a00u3046",
    "2": "test",
    "varlastname": "test",
    "3": "u5730",
    "varusername": "u5730",
    "4": "abc@gmail.com",
    "varemailid": "abc@gmail.com",
    "5": "qwert",
    "varpassword": "qwert",
    "6": "12345",
    "varmobileno": "12345",
    "7": "Enable",
    "mobileMessage": "Enable",
    "8": "",
    "varphoneno": "",
    "9": "Enable",
    "enumstatus": "Enable",
    "10": "2013-01-30",
    "date_insert": "2013-01-30",
    "11": "2013-01-30",
    "date_edit": "2013-01-30",
    "12": "210.211.252.1",
    "varipaddress": "210.211.252.1"
  }
]

转换NSArray如下:

// Create new SBJSON parser object 
SBJSON *parser = [[SBJSON alloc] init];  
NSArray *array = (NSArray *) [parser objectWithString:json_string error:nil];
NSLog(@"Array === %@",array);

Array显示数据:

Log :

Array ===
(
    {
    0 = 41;
    1 = "";
    10 = "2013-01-30";
    11 = "2013-01-30";
    12 = "210.211.252.1";
    2 = test;
    3 = "";
    4 = "abc@gmail.com";
    5 = qwert;
    6 = 12345;
    7 = Enable;
    8 = "";
    9 = Enable;
    "date_edit" = "2013-01-30";
    "date_insert" = "2013-01-30";
    enumstatus = Enable;
    intid = 41;
    mobileMessage = Enable;
    varemailid = "abc@gmail.com";
    varfirstname = "";
    varipaddress = "210.211.252.1";
    varlastname = test;
    varmobileno = 12345;
    varpassword = qwert;
    varphoneno = "";
    varusername = "";
})

转换** VarfirstName **如下:

NSDictionary *dataObject=[array objectAtIndex:0];
NSString *status = [dataObject objectForKey:@"varfirstname"];
NSLog(@"status -- %@",status);
NSString *myDecodedS = status;
NSString *myDecoded = [myDecodedS stringByReplacingPercentEscapesUsingEncoding:NSUTF16StringEncoding];
NSLog(@"HIIii %@",myDecoded);

解决方案

把json复制到text文件中,转换回数据,使用NSJSONSerialization类,

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error;
    NSData *data = [[NSData alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"]];
    id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@",jsonObj);
    NSLog(@"%@",jsonObj[0][@"varfirstname"]);
}

log输出如下:

2013-02-01 22:47:40.361 JSONExperiment[4200:c07] (
        {
        0 = 41;
        1 = "U8a00U3046";
        10 = "2013-01-30";
        11 = "2013-01-30";
        12 = "210.211.252.1";
        2 = test;
        3 = "U5730";
        4 = "abc@gmail.com";
        5 = qwert;
        6 = 12345;
        7 = Enable;
        8 = "";
        9 = Enable;
        "date_edit" = "2013-01-30";
        "date_insert" = "2013-01-30";
        enumstatus = Enable;
        intid = 41;
        mobileMessage = Enable;
        varemailid = "abc@gmail.com";
        varfirstname = "U8a00U3046";
        varipaddress = "210.211.252.1";
        varlastname = test;
        varmobileno = 12345;
        varpassword = qwert;
        varphoneno = "";
        varusername = "U5730";
    }
)
2013-02-01 22:47:40.363 JSONExperiment[4200:c07] 言う
解决方案二:

下载最新的SBJSON 3.1.1版本就解决了。

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSString *str =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str);
NSArray *array = (NSArray *) [parser objectWithString:str error:nil];
NSLog(@"Array === %@",array);

log文件:

2013-02-02 12:20:31.931 MyTab[1343:c07] [{"0":"41","intid":"41","1":"u8a00u3046","varfirstname":"u8a00u3046","2":"test","varlastname":"test","3":"u5730","varusername":"u5730","4":"abc@gmail.com","varemailid":"abc@gmail.com","5":"qwert","varpassword":"qwert","6":"12345","varmobileno":"12345","7":"Enable","mobileMessage":"Enable","8":"","varphoneno":"","9":"Enable","enumstatus":"Enable","10":"2013-01-30","date_insert":"2013-01-30","11":"2013-01-30","date_edit":"2013-01-30","12":"210.211.252.1","varipaddress":"210.211.252.1"}]
2013-02-02 12:20:31.933 MyTab[1343:c07] Array === (
        {
        0 = 41;
        1 = "U8a00U3046";
        10 = "2013-01-30";
        11 = "2013-01-30";
        12 = "210.211.252.1";
        2 = test;
        3 = "U5730";
        4 = "abc@gmail.com";
        5 = qwert;
        6 = 12345;
        7 = Enable;
        8 = "";
        9 = Enable;
        "date_edit" = "2013-01-30";
        "date_insert" = "2013-01-30";
        enumstatus = Enable;
        intid = 41;
        mobileMessage = Enable;
        varemailid = "abc@gmail.com";
        varfirstname = "U8a00U3046";
        varipaddress = "210.211.252.1";
        varlastname = test;
        varmobileno = 12345;
        varpassword = qwert;
        varphoneno = "";
        varusername = "U5730";
    }
)
【云栖快讯】诚邀你用自己的技术能力来用心回答每一个问题,通过回答传承技术知识、经验、心得,问答专家期待你加入!  详情请点击
云栖社区(yq.aliyun.com)为您免费提供ios-转换后JSON响应获取空值相关信息,包括 iosjsoniphone nsarray 的信息 ,所有ios-转换后JSON响应获取空值相关内容均不代表云栖社区的意见! 该页面h5页面的地址是:https://m.aliyun.com/yunqi/wenzhang/show_82140,您可以点击ios-转换后JSON响应获取空值-手机站访问。
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

现在注册,免费体验40+云产品,及域名优惠!

云服务器9.9元/月,大学必备

热点导航