An iOS control to display JSON and/or NSDictionary data in a friendly format.
Just drop the source files into your project.
The following code, placed in the viewDidLoad
method of a UIViewController
, displays the JSONView that is in the screenshot below.
// sample json
NSString* jsonString =
@"{"
"\"id\":12345,"
"\"company\":\"ACME\","
"\"departments\":[\"Marketing\", \"Development\", \"Sales\"],"
"\"founder\":{"
"\"name\" : {"
"\"first\" : \"John\","
"\"last\" : \"Doe\","
"},"
"\"email\" : \"[email protected]\""
"}"
"}"
;
// parse the json string into a dictionary
NSDictionary* jsonData = [NSJSONSerialization JSONObjectWithData:
[jsonString
dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:nil];
// initialize a JSONView, giving it the JSON data dictionary
JSONView* jsonView = [[JSONView alloc] initWithData:jsonData];
// optional
jsonView.layer.borderColor = [self colorWithHexString:@"808080"].CGColor;
jsonView.layer.borderWidth = 1.0f;
[self.view addSubview:jsonView];
// you provide the width constraints, but the JSONView will set its own height
[jsonView performLayoutWithWidth:self.view.frame.size.width - 40];
// set the position
[jsonView setFrame:CGRectMake(20, 100, jsonView.frame.size.width, jsonView.frame.size.height)];
I originally wrote this control as part of the implementation of the Honeybadger iOS app. This open-source port of the control is made available with permission.