This answer seems to show how to make a JSONObject.
NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
The output looks like a json object. But then I tried the following:
NSLog(@"%@", [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil ]);
What I got back was
<5b0a2020 7b0a2020 20202269 6422203a 20223122 2c0a2020 2020226e
616d6522 203a2022 41616122 0a20207d 2c0a2020 7b0a2020 20202269
6422203a 20223222 2c0a2020 2020226e 616d6522 203a2022 42626222
0a20207d 0a5d>
This seems to show that it isn’t a real JSONObject. How do you make one?
It may be a real JSONObject, but NSLog doesn’t know how to display raw data… the
"%@"bit in NSLog wants a NSString with an encoding, not NSData.There are two ways I can see off the top of my head to tell if things worked out okay.
#1) use the
[isValidJSONObject:]methodor
#2) re-parse the JSON object you just created and see if it comes out the way you created it. You can print out the NSData by doing something like: