Search code examples
iosobjective-cjsonnsdictionaryfoundation

How do I create dummy JSON data on the client in objective C / iOS?


I want to set up static dummy data, in JSON, for my app to process. This is purely client-side; I don't want to retrieve anything from the network.

All the questions and answers I've seen so far have NSData* variables storing what's retrieved from network calls and [JSONSerialization JSONObjectWithData: ...] usually acting on data that was not created manually.

Here's an example of what I've tried within xcode.

NSString* jsonData = @" \"things\": [{ \
\"id\": \"someIdentifier12345\", \
\"name\": \"Danny\" \
\"questions\": [ \
    { \
        \"id\": \"questionId1\", \
        \"name\": \"Creating dummy JSON data by hand.\" \
    }, \
    { \
        \"id\": \"questionId2\", \
        \"name\": \"Why no workie?\"
    } \
    ], \
\"websiteWithCoolPeople\": \"http://stackoverflow.com\", \
}]}";

NSError *error;
NSDictionary *parsedJsonData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

Attempts like that (and trying to change things, like switching the NSString* to NSData* from that JSON string) have yielded a null parsedJsonData data or exceptions when trying to create that JSON data variable or when trying to parse it.

How do I create dummy JSON data within my own code such that it can be parsed through the normal Foundation classes that parse JSON data?


Solution

  • I would save my test json as separate files in the app. The advantage of this is that you can just copy & paste responses from a web service and read them easily without having to convert them to NSDictionaries or escaped strings.

    I've correctly formatted your JSON (using jsonlint) and saved it to a file named testData.json in the app bundle.

    {"things":
        [{
         "id": "someIdentifier12345",
         "name": "Danny",
         "questions": [
                       {
                       "id": "questionId1",
                       "name": "Creating dummy JSON data by hand."
                       },
                       {
                       "id": "questionId2",
                       "name": "Why no workie?"
                       } 
                       ], 
         "websiteWithCoolPeople": "http://stackoverflow.com" 
         }]
    }
    

    Then in order to parse this file in your app you can simply load the file from the bundle directory.

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"testdata" ofType:@"json"];
    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
    
    NSError *error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    
    NSLog(@"%@", jsonDict);
    

    It would now be pretty easy to extend this to load any number of responses and essentially have a local web service. It then wouldn't be much more work to adapt this to load responses from a remote server.