gpt4 book ai didi

ios - 如何从json存储在sqlite数据库中

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:19 26 4
gpt4 key购买 nike

我创建了一个应用程序,我从 JSON 读取 TableView 中的许多数据,我想解析这个 JSON 并存储在 sqlite 中,但我不知道应该从哪里开始?

这是解析我的 json 代码:

@implementation TableViewController
{
NSArray *news;
NSMutableData *data;
NSString *title;
NSMutableArray *all;
}
@synthesize mainTable;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"News";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://zacandcatie.com/YouTube/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[con start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

news = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (int i =0; i < [news count]; i++)
{
NSIndexPath *indexPath = [self.mainTable indexPathForSelectedRow];
title =[[news objectAtIndex:indexPath.row+i]objectForKey:@"title"];
if (!all) {
all = [NSMutableArray array];
}
[all addObject:title];
}
NSLog(@"%@",all);

[mainTable reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"The Connection has been LOST" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

你是我的 json url。我想在 sqlite 中存储“title”和“date_string”值。请指导我!

最佳答案

以 NSDictionary 形式解析数据后,您可以创建插入查询并触发查询,然后您的数据将保存到数据库中

关于ios - 如何从json存储在sqlite数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16274598/

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