我有一个完美的MySQL数据库和PHP脚本。但是,我想知道如何将这些数据从JSON格式转换为UITextView。我还有一个表,其中包含2个其他JSON元素。谢谢!
TableInfo.h
#import <Foundation/Foundation.h>
@interface TableInfo : NSObject
@property (nonatomic, strong) NSString *Title;
@property (nonatomic, strong) NSString *SubTitle;
@property (nonatomic, strong) NSString *Description;
@end
HomeModel.h
#import <Foundation/Foundation.h>
@protocol HomeModelProtocol <NSObject>
- (void)itemsDownloaded:(NSArray *)items;
@end
@interface HomeModel : NSObject <NSURLConnectionDataDelegate>
@property (nonatomic, weak) id<HomeModelProtocol> delegate;
- (void)downloadItems;
@end
HomeModel.m
#import "HomeModel.h"
#import "TableInfo.h"
@interface HomeModel()
{
NSMutableData *_downloadedData;
}
@end
@implementation HomeModel
- (void)downloadItems
{
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://gandouhaiti.ueuo.com/service.php"];
NSLog(@"Donwload the JSON file");
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
NSLog(@"Create the request");
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
NSLog(@"Create the NSURLConnection");
}
#pragma mark NSURLConnectionDataProtocol Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Inititalize the data object");
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
[_downloadedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the locations
NSMutableArray *_locations = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new location object and set its props to JsonElement properties
TableInfo *newdata = [[TableInfo alloc] init];
newdata.Title = jsonElement[@"Title"];
newdata.SubTitle = jsonElement[@"SubTitle"];
newdata.description = jsonElement[@"Description"];
// Add this question to the locations array
[_locations addObject:newdata];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownloaded:_locations];
}
}
@end
答案 0 :(得分:0)
如果您想在故事板中的UITextView中显示JSON,请尝试以下方法:
@implementation ViewController
NSString * JSON;
-(void) viewDidLoad
{
JSON = //get data from remote;
NSString *stringForTextView = @"";
NSData *data = [JSON dataUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if([array count] >0)
{
for(NSDictionary *d in array)
{
stringForTextView = [NSString stringWithFormat:@"Title:%@\nSubTitle:%@\nDescription:%@\n", [d valueForKey:@"Title"], [d valueForKey:@"SubTitle"], [d valueForKey:@"Description"]];
}
}
_textView.text = stringForTextView;
}
@end