是否可以使用SBJson4解析NSInputStream? [英] Is it possible to parse an NSInputStream using SBJson4?

查看:132
本文介绍了是否可以使用SBJson4解析NSInputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取一个包含由NSString类型和NSMutableArrays组成的联系信息对象的JSON文件.目前,我正在使用NSData读取整个文件,然后对其进行解析.我已经使用了Stig的示例,如下所述: SBJson4Parser示例

I am trying to a read JSON file containing contact info objects consisting of NSString types and NSMutableArrays. Currently, I am using NSData to read the whole file and then parsing through it. I have utilised Stig's example as mentioned here: SBJson4Parser Example

   SBJson4ValueBlock block = ^(id obj, BOOL *stop) {
   NSLog(@"Found: %@", @([obj isKindOfClass:[NSDictionary class]]));
    //contactsData *contact = obj;
    NSDictionary *contact = obj;
    NSLog(@"Contact: %@",contact);
   /* NSString *fName, *lName;
    fName = [contact objectForKey:@"mFirstName"];
     lName = [contact objectForKey:@"mLastName"];
    NSLog(@"First Name: %@",fName);
    NSLog(@"Last Name: %@",lName);
    */
};

SBJson4ErrorBlock eh = ^(NSError* err){
    NSLog(@"Oops: %@",error);
};
NSLog(@"Parse work");
id parser = [SBJson4Parser multiRootParserWithBlock:block
                                       errorHandler:eh];
//uint8_t buf[1024];
//unsigned int len = 0;
NSLog(@"Trying to push stream to data");
//[inputStream read:buf maxLength:len];
NSData *data = [NSData dataWithContentsOfFile:filePath options:NSUTF8StringEncoding error:NULL];
//id data = [json da:NSUTF8StringEncoding];
SBJson4ParserStatus status = [parser parse:data];

NSLog(@"Status: %u",status);

如今,由于社交网络的缘故,人们似乎拥有数百甚至数千个联系人.这会导致iOS设备上的内存占用量更大吗?如果是这样,如何解析流中的单个对象?如果我必须使用一个委托,将不胜感激.

These days people seem to have hundreds or even thousands of contacts, thanks to social networks. Will this lead to a larger memory footprint on an iOS device ? If so, how do I parse a single object from a stream ? If I have to use a delegate, an example would be greatly appreciated.

请注意,我对iOS开发以及Objective-C还是陌生的.

Please note that I am new to the world of iOS development as well as Objective-C.

The structure of the json file in question:
{
  "mAddresses": [
  ],
  "mContactPhoto": "",
  "mDisplayName": ",tarun,,,,israni,,", 
  "mPhoneNumberList": [
    {
      "mLabel": "_$!<Home>!$_",
      "mNumber": "(988) 034-5678",
      "mType": 1
    }
  ]
}{
  "mAddresses": [
  ],
  "mContactPhoto": "",
  "mDisplayName": ",Sumit,,,,Kumar,,",
  "mPhoneNumberList": [
    {
      "mLabel": "_$!<Home>!$_",
      "mNumber": "(789) 034-5123",
      "mType": 1
    }
  ]
}

推荐答案

您的解决方案看起来应该对我有用.如果您的文件太大而又不想将其全部保存在内存中,即您要避免以下行:

Your solution looks like it should work to me. If your file so big that you don't want to hold it all in memory, i.e. you want to avoid this line:

NSData *data = [NSData dataWithContentsOfFile:filePath options:NSUTF8StringEncoding error:NULL];

然后,您可以使用NSInputStream(未经测试,但希望您有主旨):

then you can use an NSInputStream (untested, but hopefully you get the gist):

id parser = [SBJson4Parser multiRootParserWithBlock:block
                                       errorHandler:eh];

id is = [NSInputStream inputStreamWithFileAtPath:filePath];
[is open];

// buffer to read from the input stream
uint8_t buf[1024];

// read from input stream until empty, or an error;
// better error handling is left as an exercise for the reader
while (0 > [is read:buffer maxLength: sizeof buffer]) {
    SBJson4ParserStatus status = [parser parse:data];
    NSLog(@"Status: %u",status);
    // handle parser errors here
}
[is close];

但是,您仍然必须阅读并解析整个文件,以确保找到特定的联系人. 没有办法只读取特定的联系人.如果您经常这样做,则可能需要以其他方式存储联系人,以更好地支持这种情况.一种方法是使用例如SQLLite.

However, you still have to read and parse the whole file to guarantee that you find a particular contact. There is no way to read just a specific contact this way. If that is something you do often, you may want to store your contacts a different way that supports that scenario better. One way would be to use e.g. SQLLite.

这篇关于是否可以使用SBJson4解析NSInputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆