iPhone:如何以编程方式创建 json 文件并将从 Sqlite 数据库获取的数据插入其中?

发布于 2024-11-08 19:40:01 字数 1015 浏览 0 评论 0原文

我想从我的 Sqlite 数据库获取数据并将其放入这个 JSON 文件中。

如何创建 json 文件并将从数据库获取的数据插入到 JSON 文件中?

我想要这种格式的 JSON 文件中的数据:

{

"data": [
    {
        "data": [
            null,
            null,
            30,
            45,
            69,
            70,
            65
        ],
        "title": "title5" 
    },
    {
        "data": [
            null,
            5,
            10,
            15,
            22,
            30 
        ],
        "title": "title4" 
    },
    {
        "data": [
            40,
            55,
        ],
        "title": "title3" 
    },
    {
          "data": [
              null,
            89,
            90,
            85
        ],
        "title": "title2" 
    },
    {
        "data": [
            66,
            77,
            55,
            33,
            50,
            -6,
            -8
        ],
        "title": "title1" 
    } 
],
 "x_labels": [
    6,
    7,
    8,
    9,
    10,
    11,
    12
]
}

我该怎么做?

I want to get data from my Sqlite database and put it into this JSON file.

How can I create a json file and insert the data obtained from my database into the JSON file?

I want data in JSON file in this format:

{

"data": [
    {
        "data": [
            null,
            null,
            30,
            45,
            69,
            70,
            65
        ],
        "title": "title5" 
    },
    {
        "data": [
            null,
            5,
            10,
            15,
            22,
            30 
        ],
        "title": "title4" 
    },
    {
        "data": [
            40,
            55,
        ],
        "title": "title3" 
    },
    {
          "data": [
              null,
            89,
            90,
            85
        ],
        "title": "title2" 
    },
    {
        "data": [
            66,
            77,
            55,
            33,
            50,
            -6,
            -8
        ],
        "title": "title1" 
    } 
],
 "x_labels": [
    6,
    7,
    8,
    9,
    10,
    11,
    12
]
}

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2024-11-15 19:40:01

创建 Obj C 对象从 sqlite 数据库读取数据。假设

@interface Data:NSObject {
  NSArray *data;
  NSString *title;
}

NSArray *dataArray; // array of data filled with all Data object read from database.
  1. 使用 JSON 框架 将所有数据转换为 JSON 字符串。请注意,对于 Data 对象,您必须重写 proxyForJson 方法,因为它是用户定义的对象。

您可以从对象创建 JSON 字符串,

SBJsonWriter *writer = [SBJsonWriter new];
NSString *jsonString = [writer stringWithObject:dataArray];
[writer release];

我不确定它是否直接适用于数组,如果不能,请将您的 dataArray 封装在 anotherObject 中,然后将该对象发送给此 JSON 编写器。

将 JSON 字符串写入文件。

 [jsonString writeToFile:path atomically:YES encoding:NSASCIIStringEncoding 
             error:nil]

Create Obj C object reading data from your sqlite database. Say

@interface Data:NSObject {
  NSArray *data;
  NSString *title;
}

NSArray *dataArray; // array of data filled with all Data object read from database.
  1. Use JSON Framework to convert all data to JSON string. Notice that for the Data object, you have to override the proxyForJson method, since its a user defined object.

You can make a JSON string from an object ,

SBJsonWriter *writer = [SBJsonWriter new];
NSString *jsonString = [writer stringWithObject:dataArray];
[writer release];

I am not sure if it works directly for array, if not, encapsulate your dataArray in anotherObject, and send that object to this JSON writer.

Write the JSON string to a file.

 [jsonString writeToFile:path atomically:YES encoding:NSASCIIStringEncoding 
             error:nil]
静若繁花 2024-11-15 19:40:01

为了创建 json 输出,您可以首先获取某些数组、plist 或字典等中的 sqlite 数据,然后使用函数 JSONRepresentation 来显示相同​​的 JSON 输出,这里有一个示例代码,可能会帮助您

-(void)create_Json
{
    NSArray *arr = [[NSArray alloc] initWithObjects:@"Ravi",@"Riki",@"Faisal",@"Sanjay", nil];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:arr forKey:@"Friends"];

    NSLog(@"JSON representation for dictionary is %@",[dict JSONRepresentation]);

}

确保您正在导入 JSON 库。

In order to create a json output what you can do is first get the sqlite data in some array or plist or dictionary etc, then use the function JSONRepresentation to display the JSON output for the same, here's a sample code that might help you

-(void)create_Json
{
    NSArray *arr = [[NSArray alloc] initWithObjects:@"Ravi",@"Riki",@"Faisal",@"Sanjay", nil];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:arr forKey:@"Friends"];

    NSLog(@"JSON representation for dictionary is %@",[dict JSONRepresentation]);

}

Make sure that you are importing the JSON libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文