程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-轉換後JSON響應獲取空值

ios-轉換後JSON響應獲取空值

編輯:編程綜合問答
轉換後JSON響應獲取空值

轉換為NSArray後,JSOn響應得到的是空值

JSON Response :

JSON Log :

[
  {
    "0": "41",
    "intid": "41",
    "1": "\u8a00\u3046",
    "varfirstname": "\u8a00\u3046",
    "2": "test",
    "varlastname": "test",
    "3": "\u5730",
    "varusername": "\u5730",
    "4": "[email protected]",
    "varemailid": "[email protected]",
    "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 = "[email protected]";
    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 = "[email protected]";
    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 = "\U8a00\U3046";
        10 = "2013-01-30";
        11 = "2013-01-30";
        12 = "210.211.252.1";
        2 = test;
        3 = "\U5730";
        4 = "[email protected]";
        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 = "[email protected]";
        varfirstname = "\U8a00\U3046";
        varipaddress = "210.211.252.1";
        varlastname = test;
        varmobileno = 12345;
        varpassword = qwert;
        varphoneno = "";
        varusername = "\U5730";
    }
)
2013-02-01 22:47:40.363 JSONExperiment[4200:c07] 言う
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved