【发布时间】:2014-06-07 02:48:49
【问题描述】:
我有一个完美运行的 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
【问题讨论】:
-
我尝试使用这两种方法,但都没有成功。
-
在
NSJSONSerialization行之后添加NSLog( @"%@", jsonArray );并将结果发布在此处。也看看这篇文章,看看它是否有帮助cant-get-head-around-parsing-nested-json -
当我添加
NSLog( @"%@", jsonArray );时,我得到以下信息。( { Description = "Description Test"; SubTitle = "SubTitle Test"; Title = "Title Test"; } )这正是我的 JSON。 @user3386109 -
我明白了,所以问题不在于解析 JSON,而是将结果放入 UITextView。为此,请使用 NSString 的
stringWithFormat方法创建一个包含 JSON 信息的字符串。您可以使用\n在文本中强制换行。然后使用 UITextView 的.text属性在文本视图中显示字符串。
标签: mysql objective-c json uitextview