Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone


iOS/iPhone Passing JSON (NSJSONSerialization) Between View

iOS/iPhone Passing JSON (NSJSONSerialization) Between View การใช้งาน JSON บน iOS นั้นจะมีความจำเป็นมากในกรณีที่ต้องการเขียน Application ติดต่อกับ Interface กับโปรแกรมอื่น ๆ เช่น การรับส่งข้อมูลระหว่าง Web Server ที่มีการใช้ JSON เป็นตัวกลาง และใน iOS ที่เขียนด้วยภาษา Objective-C ก็สามารถที่จะประยุกต์ใช้ได้ทั้ง 2 รูปแบบ คือ สามารถที่จะรับค่า JSON จาก Interface อื่น ๆ และสามารถที่จะแปลงข้อมูลให้อยู่ในรูปแบบของ JSON และส่งค่าไปยัง Inteface Application อื่น ๆ ได้เช่นเดียวกัน สำหรับการสร้างและอ่าน JSON นั้น สามารถอ่านเพิ่มเติมได้ที่บทความของ JSON กับ iOS ครับ

สำหรับบทความ JSON อ่านเพิ่มเติมได้ที่นี่


iOS/iPhone Passing JSON (NSJSONSerialization) Between View

iOS/iPhone Passing JSON (NSJSONSerialization) Between View


ในบทความนี้อาจะยังไม่ได้ประยุกต์ถึงขั้นการส่งค่า JSON ไปยัง Interface อื่น แต่จะยกตัวอย่างการส่งค่า JSON ระหว่าง View เท่านั้น แต่ก็ไม่ใช่เรื่องยากที่จะนำไป Apply ใช้ เพราะเพียงแค่เข้าใจ Concept การทำงานของมัน ก็สามารถที่จะนำไปใช้ได้อย่างง่ายดาย

iOS/iPhone Passing Data Between View (Objective-C,iPhone,iPad)


การเข้ารหัส JSON ส่งค่า JSON ไปยัง View อื่น
01.NSMutableDictionary *element = [NSMutableDictionary dictionary];
02.[element setObject:[txtName text] forKey:@"Name"];
03.[element setObject:[txtTel text] forKey:@"Tel"];
04.[element setObject:[txtEmail text] forKey:@"Email"];
05.NSData *jsonData = [NSJSONSerialization dataWithJSONObject:element
06.                                                   options:NSJSONWritingPrettyPrinted
07.                                                     error:nil];
08.NSLog(@"jsonData= %@",jsonData);
09. 
10.NSString* jsonCode = [[[NSString alloc] initWithData:jsonData
11.                                          encoding:NSUTF8StringEncoding] autorelease];
12.NSLog(@"jsonCode= %@",jsonCode);
13. 
14.DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
15.view2.json = jsonCode;
16.[self presentViewController:view2 animated:NO completion:NULL];

การรับค่า JSON และแสดงค่า JSON
01.NSString *stringData = [self.json description];
02. 
03.NSData *jsonData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
04. 
05.id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
06. 
07.// value in key name
08.NSString *strName = [jsonObjects objectForKey:@"Name"];
09.NSString *strTel = [jsonObjects objectForKey:@"Tel"];
10.NSString *strEmail = [jsonObjects objectForKey:@"Email"];
11. 
12.lblName.text = strName;
13.lblTel.text = strTel;
14.lblEmail.text = strEmail;




Example ตัวอย่างการสร้าง JSON การอ่าน JSON และการส่งค่าระหว่าง View (JSON Between View)

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

เริ่มต้นด้วยการสร้าง Application บน Xcode แบบ Single View Application

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

เลือกและไม่เลือกรายการดังรูป

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

ตอนนี้ใน Project มีไฟล์ ที่เป็น Interface และ Class อยู่ 1 ชุด ซึ่งต่อไปนี้จะเรียกว่า View 1

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

สร้างขึ้นมาอีก 1 ชุด ซึ่งตอนนี้เป็น 2 ชุดแล้ว และเรียกว่า View 2

iOS/iPhone Multiple View (Objective-C, iPhone, iPad)


iOS/iPhone Passing JSON (NSJSONSerialization) Between View

ใน View 1 ให้ออกแบบหน้าจอ Interface ดังรูป

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

ใน Class ของ .h ของ View 1 ให้ทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อย

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

ใน View 2 ให้ออกแบบหน้าจอ Interface ดังรูป

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

ใน Class ของ .h ของ View 2 ให้ทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อย

จากนั้นให้เขียน Code ทั้งหมดดังนี้

ViewController.h
01.//
02.//  ViewController.h
03.//  passingJSONView
04.//
05.//  Created by Weerachai on 12/4/55 BE.
06.//  Copyright (c) 2555 Weerachai. All rights reserved.
07.//
08. 
09.#import <UIKit/UIKit.h>
10. 
11.@interface ViewController : UIViewController
12.{
13.    IBOutlet UITextField *txtName;
14.    IBOutlet UITextField *txtTel;
15.    IBOutlet UITextField *txtEmail;
16.}
17. 
18.- (IBAction)btnSubmit:(id)sender;
19. 
20.@end


ViewController.m
01.//
02.//  ViewController.m
03.//  passingJSONView
04.//
05.//  Created by Weerachai on 12/4/55 BE.
06.//  Copyright (c) 2555 Weerachai. All rights reserved.
07.//
08. 
09.#import "ViewController.h"
10. 
11.#import "DetailController.h"
12. 
13.@interface ViewController ()
14. 
15.@end
16. 
17.@implementation ViewController
18. 
19.- (void)viewDidLoad
20.{
21.    [super viewDidLoad];
22.    // Do any additional setup after loading the view, typically from a nib.
23.}
24. 
25.- (void)didReceiveMemoryWarning
26.{
27.    [super didReceiveMemoryWarning];
28.    // Dispose of any resources that can be recreated.
29.}
30. 
31.- (IBAction)btnSubmit:(id)sender {
32.     
33.    NSMutableDictionary *element = [NSMutableDictionary dictionary];
34.    [element setObject:[txtName text] forKey:@"Name"];
35.    [element setObject:[txtTel text] forKey:@"Tel"];
36.    [element setObject:[txtEmail text] forKey:@"Email"];
37.    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:element
38.                                                       options:NSJSONWritingPrettyPrinted
39.                                                         error:nil];
40.    NSLog(@"jsonData= %@",jsonData);
41.     
42.    NSString* jsonCode = [[[NSString alloc] initWithData:jsonData
43.                                              encoding:NSUTF8StringEncoding] autorelease];
44.    NSLog(@"jsonCode= %@",jsonCode);
45.     
46.    DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
47.    view2.json = jsonCode;
48.    [self presentViewController:view2 animated:NO completion:NULL];
49.}
50. 
51.- (void)dealloc {
52.    [txtName release];
53.    [txtTel release];
54.    [txtEmail release];
55.    [super dealloc];
56.}
57. 
58.@end




DetailController.h
01.//
02.//  DetailController.h
03.//  passingJSONView
04.//
05.//  Created by Weerachai on 12/4/55 BE.
06.//  Copyright (c) 2555 Weerachai. All rights reserved.
07.//
08. 
09.#import "ViewController.h"
10. 
11.@interface DetailController : ViewController
12.{
13.    IBOutlet UILabel *lblName;
14.    IBOutlet UILabel *lblTel;
15.    IBOutlet UILabel *lblEmail;
16.}
17. 
18.@property (strong, nonatomic) id json;
19. 
20.@end


DetailController.m
01.//
02.//  DetailController.m
03.//  passingJSONView
04.//
05.//  Created by Weerachai on 12/4/55 BE.
06.//  Copyright (c) 2555 Weerachai. All rights reserved.
07.//
08. 
09.#import "DetailController.h"
10. 
11.@interface DetailController ()
12. 
13.@end
14. 
15.@implementation DetailController
16. 
17.- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
18.{
19.    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
20.    if (self) {
21.        // Custom initialization
22.    }
23.    return self;
24.}
25. 
26.- (void)viewDidLoad
27.{
28.    [super viewDidLoad];
29.    // Do any additional setup after loading the view from its nib.
30.     
31.    NSString *stringData = [self.json description];
32.     
33.    NSData *jsonData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
34.     
35.    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
36.     
37.    // value in key name
38.    NSString *strName = [jsonObjects objectForKey:@"Name"];
39.    NSString *strTel = [jsonObjects objectForKey:@"Tel"];
40.    NSString *strEmail = [jsonObjects objectForKey:@"Email"];
41.    //NSLog(@"MemberID = %@",strName);
42.    //NSLog(@"Name = %@",strTel);
43.    //NSLog(@"Tel = %@",strEmail);
44.     
45.    lblName.text = strName;
46.    lblTel.text = strTel;
47.    lblEmail.text = strEmail;
48.   
49.}
50. 
51.- (void)didReceiveMemoryWarning
52.{
53.    [super didReceiveMemoryWarning];
54.    // Dispose of any resources that can be recreated.
55.}
56. 
57.- (void)dealloc {
58.    [lblName release];
59.    [lblTel release];
60.    [lblTel release];
61.    [lblEmail release];
62.    [super dealloc];
63.}
64.@end


Screenshot

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

แสดง View 1 เมื่อคลิกที่ Button จะมีการนำข้อมูลที่ได้เจ้ารหัสเป็น JSON และส่งไปยัง View 2

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

View 2 รับค่า JSON และอ่านค่า JSON เพื่อแสดงบน Label

iOS/iPhone Passing JSON (NSJSONSerialization) Between View

ใน NSLog จะเห็นว่าจะมีการเข้ารหัส JSON ซึ่งปกติแล้ว เมื่อใช้ NSData ในการเข้ารหัส JSON ด้วย NSJSONSerialization จะไม่สามารถอ่านข้อความเหล่านั้นออกได้ ซึ่งจะต้องแปลงให้เป็น NSString เสียก่อน



เพิ่มเติม
บทความนี้ได้เน้นที่จะให้เข้าใจเกี่ยวกับการรับค่าจาก Input และการเข้ารหัส JSON และตัวอย่างการส่งค่า JSON ไปยัง View อื่น ๆ เพื่อให้เป็นแนวทางในการที่จะพัฒนาโปรแกรมเกี่ยวกับการรับส่งข้อมูลในรูปแบบ JSON ให้มากยิ่งขึ้น และเราสามารถที่จะนำความรู้นี้ไป Apply ใช้กับการเขีนโปรแกรมอื่น ๆ เช่น การส่งค่า JSON ไปยัง Interface ของ Application อื่น ๆ ที่อยู่คนล่ะ Platform หรืออยู่บน Web Server เป็นต้น

   
Hate it
Don't like it
It's ok
Like it
Love it
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-06 10:34:37 / 2017-03-26 09:05:13
  Download : Download  iOS/iPhone Passing JSON (NSJSONSerialization) Between View
 Sponsored Links / Related

 
iOS/iPhone and JSON (Create JSON and JSON Parsing, Objective-C)
Rating :

 
iOS/iPhone Table View and JSON (UITableView from JSON Parser)
Rating :

 
iOS/iPhone PHP/MySQL and JSON Parsing (Objective-C)
Rating :

 
iOS/iPhone Display Image on Table View from JSON URL (Web Site)
Rating :

 
iOS/iPhone Search Bar (UISearchBar) Data from Web Server Using JSON
Rating :

 
iOS/iPhone Picker View and JSON (UIPickerView)
Rating :

 
iOS/iPhone Collection View (UICollectionView) and JSON (Web Server URL)
Rating :

 
iOS/iPhone XML Parser / XML Feed from URL (NSXMLParser ,Objective-C)
Rating :

 
iOS/iPhone NSURLConnection and PHP MySQL / JSON (TableView,UITableView)
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่