2

我创建了一个应用程序,我从 JSON 读取表视图中的许多数据,我想解析这个 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 网址。我想在 sqlite 中存储“title”和“date_string”值。请指导我!!!

4

5 回答 5

1
-(void)InsertRecords:(NSMutableDictionary *)dict
{

sqlite3_stmt *stmt;
sqlite3 *cruddb;

NSMutableString *str = [NSMutableString stringWithFormat:@"Insert into tblName ("];

for (int i = 0; i<[[dict allKeys] count]; i++)
{
    [str appendFormat:@"%@,",[[dict allKeys] objectAtIndex:i]];
}
[str appendFormat:@")values ("];
for (int i = 0; i<[[dict allKeys] count]; i++)
{
    [str appendFormat:@"%@,",[dict valueForKey:[[dict allKeys] objectAtIndex:i]]];
}

[str appendFormat:@");"];
NSLog(@"qry : %@",str);
const char *sql = [str UTF8String]; ;

if((sqlite3_open([database UTF8String], &cruddb)==SQLITE_OK))
{
    if (sqlite3_prepare(database, sql, -1, &stmt, NULL) ==SQLITE_OK)
    {
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
    }
    else
    {
        NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(database));
    }
    sqlite3_close(database);
}
else
{
    NSLog(@"An error has occured: %s",sqlite3_errmsg(database));
}
}

尝试这个。

于 2013-04-29T09:53:12.297 回答
1

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

于 2013-04-29T09:13:01.937 回答
0

你可以做这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  tableview cell setup

    NSArray* keys = [self.data allKeys];

    cell.textLabel.text = [self.data objectForKey:[keys objectAtIndex:indexPath.row]];

    return cell;
}

请参考链接以在字典中按顺序排列数据

带有有序键的 NSDictionary

于 2013-04-29T08:59:25.933 回答
0

继续@Divz Ans...

您将创建 .sqlite 文件。没有比这更容易的了。

有两种方法(我知道)来创建 sqlite 文件,

1>您可以在firefox中下载SQLite Manager插件,您可以在其中以图形方式操作数据库中的数据。

或者,

2> 您可以将终端与单行命令sqlite3 dbFileName.sqlite 一起使用。进入,

您将在哪里获得sqlite>现在从进一步的 SQL(create/insert/update..) 查询开始。

你可以在 MacHD>users>admin(not shared one)>yourFile.sqlite 或者 finder---go>home>yourFile.sqlite 找到你的 sqlite 文件

于 2013-05-17T10:36:58.680 回答
0
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        db=[[SKDatabase alloc]initWithFile:@"student.sqlite"];
        NSURL *url=[NSURL URLWithString:@"..........Your Url............"];
        NSURLRequest *json_request=[[NSURLRequest alloc]initWithURL:url];
        NSData *data=[NSURLConnection sendSynchronousRequest:json_request returningResponse:nil error:nil];
        NSMutableDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *student_ary=[dic objectForKey:@"students"];

        for (NSMutableArray *student_info in student_ary) {
            NSMutableDictionary *insert=[[NSMutableDictionary alloc]initWithCapacity:2];
            NSMutableDictionary *info=[student_info mutableCopy];
            [insert setObject:[info objectForKey:@"name"] forKey:@"name"];
            [insert setObject:[info objectForKey:@"city"] forKey:@"city"];
            [db insertDictionary:insert forTable:@"student_info"];

        }
    })

//.m 文件查看....

-(void)viewDidAppear:(BOOL)animated
{
    NSString *qry=@"select * from student_info";
    ary=[[db lookupAllForSQL:qry] mutableCopy];
    [tableView reloadData];
}
于 2014-05-07T18:30:07.500 回答