php - 表的 FMDB 结果到 JSON 对象,遵循列的顺序。

标签 php ios objective-c json fmdb

编辑:底部的新问题它得到了我错的所有行 我正在尝试获取 SQL 查询的结果并将它们放入一个 json 对象中以发送到我的服务器以对数据进行更多处理。现在我的代码只返回 1 行数据。任何人都可以看到任何明显的错误吗? iOS端

- (void)sendLogin
{
NSError *jsonError;
NSData *requestdata;
//get login
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir   stringByAppendingPathComponent:@"tar.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
FMResultSet *Loginresults = [database executeQuery:@"SELECT * FROM surveys"];
NSMutableArray *results = [NSMutableArray array];
while ([Loginresults next]) {
    [results addObject:[Loginresults resultDictionary]];

    requestdata = [NSJSONSerialization dataWithJSONObject:results options:0 error:&jsonError];
}

[database close];




NSURL *url = [NSURL URLWithString:@"http://server/insert.php"];

NSMutableURLRequest *request;
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestdata];

//this kicks off the request asynchronously
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

PHP 端

<?php
$foo = file_get_contents("php://input");

var_dump(json_decode($foo, true));

?>

由此产生的结果

finish requesting: array(1) {
[0]=>
array(7) {
["desc"]=>
string(13) "matt the best"
["creator"]=>
string(7) "eric jr"
["synch"]=>
NULL
["name"]=>
string(4) "matt"
["sid"]=>
string(1) "1"
["datetime"]=>
string(3) "now"
["pid"]=>
string(1) "1"
}
}

我还注意到该表的结果顺序不正确。这是按预期工作吗?有什么方法可以对其进行排序,使其与列的顺序相匹配吗?

最佳答案

好的。让我解释一下解决方案。正如您在评论中所说,您在整个应用程序中都在使用该库。

因此,我认为解决您的问题的最佳方法是创建 OrderedDictionary from CocoaWithLove . OrderedDictionary 包含一个 NSMutableDictionary 和一个 NSMutableArray 用于其中的键,以跟踪键的顺序。

您可以创建一个 FMResultSet 类别:

FMResultSet+OrderedDict.h

#import <Foundation/Foundation.h>
#import "FMResultSet.h"
#import "OrderedDictionary.h"
@interface FMResultSet(OrderedDict)
- (OrderedDictionary*)resultOrderDictionary;
@end

FMResultSet+OrderedDict.m

#import "FMResultSet+OrderedDict.h"
#import "OrderedDictionary.h"
#import "FMDatabase.h"

@implementation FMResultSet(OrderedDict)

- (OrderedDictionary*)resultOrderDictionary {

    NSUInteger num_cols = (NSUInteger)sqlite3_data_count([_statement statement]);

    if (num_cols > 0) {
        OrderedDictionary *dict = [OrderedDictionary dictionaryWithCapacity:num_cols];

        int columnCount = sqlite3_column_count([_statement statement]);

        int columnIdx = 0;
        for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {

            NSString *columnName = [NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)];
            id objectValue = [self objectForColumnIndex:columnIdx];
            [dict setObject:objectValue forKey:columnName];
        }

        return dict;
    }
    else {
        NSLog(@"Warning: There seem to be no columns in this set.");
    }

    return nil;
}

@end

在你的代码中这样做:

NSMutableArray *results = [NSMutableArray array];
    while ([Loginresults next]) {
        [results addObject:[Loginresults resultOrderDictionary]];
    }
    requestdata = [NSJSONSerialization dataWithJSONObject:results options:0 error:&jsonError];

如果你愿意,我可以将我的项目测试上传到 github。

编辑:code现在已上传。

关于php - 表的 FMDB 结果到 JSON 对象,遵循列的顺序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20083100/

相关文章:

php - 在 php 中只运行一次更新查询

返回整数(32 位 int)的 PHP 哈希函数

带有罗盘和位置的 iOS 增强现实

php 分解一个数组(一列)并将其放入 mysql(不同的列)

PHP - MySQL 查询拒绝执行

ios - Facebook Graph API - 标记照片

ios - 有没有办法检查观察者是否在听一些 NSNotification?

ios - 为什么 CGContextSetStrokeColorWithColor 不将文本颜色设置为黑色?

ios - 将圆角半径添加到 UITableViewCell 中的 ImageView 时出现问题

ios - 如何知道 UILabel 中的单词是否被剪切