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



Clound SSD Virtual Server

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 อื่น
    NSMutableDictionary *element = [NSMutableDictionary dictionary];
    [element setObject:[txtName text] forKey:@"Name"];
    [element setObject:[txtTel text] forKey:@"Tel"];
    [element setObject:[txtEmail text] forKey:@"Email"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:element
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    NSLog(@"jsonData= %@",jsonData);
    
    NSString* jsonCode = [[[NSString alloc] initWithData:jsonData
                                              encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"jsonCode= %@",jsonCode);
    
    DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
    view2.json = jsonCode;
    [self presentViewController:view2 animated:NO completion:NULL];

การรับค่า JSON และแสดงค่า JSON
    NSString *stringData = [self.json description];
    
    NSData *jsonData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    
    // value in key name
    NSString *strName = [jsonObjects objectForKey:@"Name"];
    NSString *strTel = [jsonObjects objectForKey:@"Tel"];
    NSString *strEmail = [jsonObjects objectForKey:@"Email"];
    
    lblName.text = strName;
    lblTel.text = strTel;
    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
//
//  ViewController.h
//  passingJSONView
//
//  Created by Weerachai on 12/4/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UITextField *txtName;
    IBOutlet UITextField *txtTel;
    IBOutlet UITextField *txtEmail;
}

- (IBAction)btnSubmit:(id)sender;

@end


ViewController.m
//
//  ViewController.m
//  passingJSONView
//
//  Created by Weerachai on 12/4/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

#import "DetailController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnSubmit:(id)sender {
    
    NSMutableDictionary *element = [NSMutableDictionary dictionary];
    [element setObject:[txtName text] forKey:@"Name"];
    [element setObject:[txtTel text] forKey:@"Tel"];
    [element setObject:[txtEmail text] forKey:@"Email"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:element
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    NSLog(@"jsonData= %@",jsonData);
    
    NSString* jsonCode = [[[NSString alloc] initWithData:jsonData
                                              encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"jsonCode= %@",jsonCode);
    
    DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
    view2.json = jsonCode;
    [self presentViewController:view2 animated:NO completion:NULL];
}

- (void)dealloc {
    [txtName release];
    [txtTel release];
    [txtEmail release];
    [super dealloc];
}

@end









DetailController.h
//
//  DetailController.h
//  passingJSONView
//
//  Created by Weerachai on 12/4/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface DetailController : ViewController
{
    IBOutlet UILabel *lblName;
    IBOutlet UILabel *lblTel;
    IBOutlet UILabel *lblEmail;
}

@property (strong, nonatomic) id json;

@end


DetailController.m
//
//  DetailController.m
//  passingJSONView
//
//  Created by Weerachai on 12/4/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "DetailController.h"

@interface DetailController ()

@end

@implementation DetailController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *stringData = [self.json description];
    
    NSData *jsonData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    
    // value in key name
    NSString *strName = [jsonObjects objectForKey:@"Name"];
    NSString *strTel = [jsonObjects objectForKey:@"Tel"];
    NSString *strEmail = [jsonObjects objectForKey:@"Email"];
    //NSLog(@"MemberID = %@",strName);
    //NSLog(@"Name = %@",strTel);
    //NSLog(@"Tel = %@",strEmail);
    
    lblName.text = strName;
    lblTel.text = strTel;
    lblEmail.text = strEmail;
  
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [lblName release];
    [lblTel release];
    [lblTel release];
    [lblEmail release];
    [super dealloc];
}
@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 เป็นต้น

   
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
   







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