-4

I have an NSMutableArray that holds a collection of objects, with each object having three parameters of type NSString. What I need to do is iterate through this collection, and create a single JSON document with two subsections: one is called "Test Results", the other is called, "Reports". Each object in the NSMutable array has three NSString parameters, and one of these parameters is called, "testResult".

If "testResult" has a value of either "pass" or "fail", then that object needs to go into the "Test Result" section. If the parameter has a value of "na", then the object goes into the "Reports" section. The "Test Results" section should have three "elements": "Name", "Date", "Test Result". The "Reports" section needs to have only two elements: "Name", and "Date". My issue is not how to iterate through an NSMutableArray using a for loop, my issue is how to iterate through an NSMutableArray, and construct a JSON document as I have described above.

My code would be something like:

    //creating beginning of JSON document here

    for (Person *personObject in testResultArray) {

         if (personObject.testResult == @"na") {

             //construct JSON object for "Reports" section
             NSString *jsonString1 = personObject.name;
             NSString *jsonString2 = personObject.date;
NSData *data1 = [jsonString1 dataUsingEncoding:NSUTF8StringEncoding];
NSData *data2 = [jsonString2 dataUsingEncoding:NSUTF8StringEncoding];
NSError * error = nil;
id json1 = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:&error];
id json2 = [NSJSONSerialization JSONObjectWithData:data2 options:0 error:&error];

         } 
         else {

            //construct JSON object for "Test Results" section
            NSString *jsonString1 = personObject.name;
            NSString *jsonString2 = personObject.date;
            NSString *jsonString3 = personObject.testResult;

NSData *data1 = [jsonString1 dataUsingEncoding:NSUTF8StringEncoding];
NSData *data2 = [jsonString2 dataUsingEncoding:NSUTF8StringEncoding];
NSData *data3 = [jsonString3 dataUsingEncoding:NSUTF8StringEncoding];

NSError * error = nil;
id json1 = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:&error];
id json2 = [NSJSONSerialization JSONObjectWithData:data2 options:0 error:&error];
id json3 = [NSJSONSerialization JSONObjectWithData:data3 options:0 error:&error];

         }

    }

    //close the JSON document construction, and return JSON document.
halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74
  • 2
    This is where you employ the lost art of actually programming. What have you tried so far? – Hot Licks Dec 24 '12 at 22:31
  • To add to @HotLicks comment. It seems you have thought about how you want to build this object, and you have obvioulsy decided for some reason to serialize the object using JSON. When going about that process of deciding to use JSON, did you not find out how to actually serialize to JSON? – Mike Brant Dec 24 '12 at 22:48
  • This is part of the problem. I unfortunately have never worked with JSON, and most of the tutorials that I have come across deal with parsing JSON documents that are being retrieved from the web, as opposed to constructing specific JSON documents from an array. I have provided some additional code with respect to converting the data to JSON format, but as I said in my question, I am not sure how to create a JSON document with the two subsections, and having elements constructed as I described. – syedfa Dec 24 '12 at 23:13
  • To construct a JSON string you reverse the process -- create and populate the language objects (eg, NS(Mutable)Array, NS(Mutable)Dictionary), then use the "mirror" routine to the deserialize routine to serialize the objects into JSON. You have to be careful to construct the source objects with legal JSON "stuff", though -- NSStrings, NSNumbers, and other NSArrays and NSDictionarys. – Hot Licks Dec 25 '12 at 16:00
  • (Note that you should never need to go from NSString to NSData to deserialize JSON -- every JSON toolset I know of can read directly from NSString.) – Hot Licks Dec 25 '12 at 16:03
  • (I take that back -- Apple's stupid JSON implementation requires a NSData object. You're much better off using a 3rd-party tool.) – Hot Licks Dec 25 '12 at 16:16

1 Answers1

0

Roughly:

//creating beginning of JSON document here

NSMutableArray* reports = [NSMutableArray array];
NSMutableArray* results = [NSMutableArray array];

for (Person *personObject in testResultArray) {

     if (personObject.testResult == @"na") {

         NSMutableDictionary* naPerson = [NSMutableDictionary dictionary];
         [naPerson setObject:personObject.name forKey:@"name"];
         [naPerson setObject:personObject.date forKey:@"date"];
         [reports addObject:naPerson];
     } 
     else {
         NSMutableDictionary* person = [NSMutableDictionary dictionary];
         [person setObject:personObject.name forKey:@"name"];
         [person setObject:personObject.date forKey:@"date"];
         [person setObjeect:personObject.testResult forKey:@"testResult"];

         [results addObject:person];
     }

}

NSMutableDictionary* mainDoc = [NSMutableDictionary dictionary];
[mainDoc setObject:reports forKey:@"reports"];
[mainDoc setObject:results forKey:@"results"];

NSString* JSONString = [JSONToolKit stringFromObject:mainDoc];

Note that Apple's NSJSONSerialization package is brain-dead, requiring you to uselessly cycle through an NSData representation. Use a different package -- pick one at http://www.json.org/

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Thanks very much for your reply. I actually was thrown off by many of the examples because many use different JSON third party libraries, and in turn each is using them to demonstrate different functionality. That being said, if I decide to stay with Apple's native JSON library, the only thing I would really have to do differently from your example is convert mainDoc to an NSData object, which in turn, I would convert to the JSON object from where I would pull my results, correct? – syedfa Dec 25 '12 at 20:16
  • I got it working to do what i needed to. Thanks very much for your time, and help. – syedfa Dec 25 '12 at 22:02
  • You are correct (so far as I know) about how to use the Apple API. But there are several other kits that work very well and are less awkward to use, so I don't really recommend the Apple stuff. Regardless of the kit used there are really only two interfaces: Language objects -> JSON string, and JSON string to language objects. Some "dress it up" (or obfuscate, depending on point of view) by having different verbs for, say, arrays and dictionaries, but that's basically just meaningless syntactic sugar. – Hot Licks Dec 26 '12 at 03:49