轉換為NSArray後,JSOn響應得到的是空值的解決方案

來源:互聯網
上載者:User

原問題描述:轉換為NSArray後,JSOn響應得到的是空值JSON Response :JSON Log :[plain]  [    {      "0": "41",      "intid": "41",      "1": "\u8a00\u3046",      "varfirstname": "\u8a00\u3046",      "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如下:[plain] // Create new SBJSON parser object   SBJSON *parser = [[SBJSON alloc] init];    NSArray *array = (NSArray *) [parser objectWithString:json_string error:nil];  NSLog(@"Array === %@",array);    Array顯示資料:Log :[plain]  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 **如下:[plain]  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類,[plain]  - (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輸出如下:[plain]  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 = "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 = "\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] 言う     

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.