gpt4 book ai didi

mysql - 如何将 JSON 元素放入 UITextView 中?

转载 作者:行者123 更新时间:2023-11-30 00:10:42 26 4
gpt4 key购买 nike

我有一个可以完美运行的 MySQL 数据库和 PHP 脚本。但是,我想知道如何将这些数据从 JSON 格式获取到 UITextView。我还有一个表,其中填充了另外 2 个 JSON 元素。谢谢!

表信息.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

最佳答案

如果您想在 Storyboard 中的 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

关于mysql - 如何将 JSON 元素放入 UITextView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24093166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com