0

I have a file with the following format:

/Users/devplayerx/Sandbox/pics/images/001012DG-161.JPG
  pixelWidth: 1600 
  pixelHeight: 1050 
filename: 001012DG-161.JPG

/Users/devplayerx/Sandbox/pics/images/001019DG-151 COPY.JPG
  pixelWidth: 1600 
  pixelHeight: 1050 
filename: 001019DG-151 COPY.JPG

and would like to, ultimately, have an iOS dictionary with the filename as key, and either a dictionary or array with the pixelWidth and pixelHeight as value. I was considering converting my text file into a JSON file, and then parse it using NSJSONSerialization, but I'm not sure how to convert my text file into JSON. Also, I'd like to remove the full path from the text file, since it's not needed.

1
  • Just to be sure, you mean that this text file is static, known at compile time? This text file won't be served by a WebService at runtime for example (thus, you can convert this text file to whatever format you want once and for all and include the converted file in the final application)? You don't want to convert it using ObjC code at runtime, that's right? Commented Oct 8, 2012 at 16:40

3 Answers 3

1

Here is a perl script that seems to do the job:

#!/usr/bin/perl
use strict;
use warnings;

open FILE,"< yourfile.txt" or die "I/O error : $!\n";
my $w = 0;
my $h = 0;
my $f = "";

print "{\n";
while (my $line = <FILE>)
{
    if ($f)
    {
        print ",\n";
        $f = "";
    }
    if ($line =~ /pixelWidth: ([0-9]+)/)
    {
        $w = $1;
    }
    if ($line =~ /pixelHeight: ([0-9]+)/)
    {
        $h = $1;
    }
    if ($line =~ /filename: (.*)$/)
    {
        $f = $1;

        print "\t\"$f\" : [ $w, $h ]"
    }
}
print "\n}\n";
close FILE;

Note that I'm not an expert in perl so maybe it can be improved, but using it on your input file seems to produce your expected JSON as below:

prompt$ perl scr.pl
{
    "001012DG-161.JPG" : [ 1600, 1050 ],
    "001019DG-151 COPY.JPG" : [ 1600, 1050 ]
}

Note that once you have your JSON file, you may optionally convert it into a PLIST file using the plutil tool. For example perl scr.pl | plutil -convert binary1 -o yourfile.plist - will create yourfile.plist from the JSON produced by perl scr.pl (my script above). You can then easily read this file in your code using [NSDictionary dictionaryWithContentsOfFile:pathToYourFilePlist] and directly have access to your data as an NSDictionary in one line.

Sign up to request clarification or add additional context in comments.

Comments

0

The json object could look like this:

{ picture: { path : "thePath", pixelWidth: 1600, pixelHeight: 1050, filename : "name" }}

I would then convert it by taking the rows and putting them in a list, looping through the list and eventually spitting everything out in another text file using the above object notation.

What language do you need to write the tooling to convert it to json in?

1 Comment

I generated the file using bash, and just removed the lines with the full path using Python. I'm left with filename, pixelHeight, and pixelWidth, which I need to convert to JSON. bash or Python would be great for that.
0

You can use NSInputStream to read the text file, on each iteration you can build your dictionary the way you want.

After that just use NSJSONSerialization.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.