I'm having an issue parsing JSON from a PHP server using NSJSONSerialization. JSLint says my JSON is valid but appears to only be able to get one-two levels in.
This is essentially my JSON structure:
{
"products":
[{
"product-name":
{
"product-sets":
[{
"set-3":
{
"test1":"test2",
"test3":"test4"
},
"set-4":
{
"test5":"test6",
"test7":"test8"
}
}]
},
"product-name-2":
{
"product-sets":
[{
}]
}
}]
}
and here is my code to parse it:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if (json) {
NSArray *products = [json objectForKey:#"products"]; // works
for (NSDictionary *pItem in products) { // works
NSLog(#"Product: %#", pItem); // works, prints the entire structure under "product-name"
NSArray *productSets = [pItem objectForKey:#"product-sets"]; // gets nil
for (NSDictionary *psItem in productSets) {
// never happens
}
}
}
I've been spinning my wheels on this for several hours, but I'm not finding anything similar anywhere I search. Are there any limitations that I'm unaware of, or am I just not seeing something obvious?
you missed one nested object
NSArray *productSets = [[pItem objectForKey:#"product-name"] objectForKey:#"product-sets"];
I tested it with this CLI program
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSString *jsonString = #"{\"products\":[{\"product-name\": {\"product-sets\": {\"set-3\":{\"test1\":\"test2\", \"test3\":\"test4\"}, \"set-4\":{\"test5\":\"test6\", \"test7\":\"test8\"} }}}, {\"product-name-2\": \"2\"}]}";
// insert code here...
NSLog(#"%#", jsonString);
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if (json) {
NSArray *products = [json objectForKey:#"products"]; // works
for (NSDictionary *pItem in products) { // works
NSLog(#"Product: %#", pItem); // works, prints the entire structure under "product-name"
NSArray *productSets = [[pItem objectForKey:#"product-name"] objectForKey:#"product-sets"]; // gets nil
for (NSDictionary *psItem in productSets) {
NSLog(#"%#", psItem);
}
}
}
}
return 0;
}
Note, that some things in your json are quite strange:
for each flattened object the keys should be the same. keys, that include a number o an object do not make much sense. If you need to keep track of single objects, include an id key with a proper value.
Related
I receive this JSON string from a web process
{
"result":"ok",
"description":"",
"err_data":"",
"data":[
{
"id":"14D19A9B-3D65-4FE2-9ACE-4C2D708DAAD8"
},
{
"id":"8BFD10B8-F5FD-4CEE-A307-FE4382A0A7FD"
}
]
}
and when I use the following to get the data:
NSError *jsonError = nil;
NSData *objectData = [ret dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json= [NSJSONSerialization JSONObjectWithData: objectData options:kNilOptions error: &jsonError];
NSLog(#"data: %#",[json objectForKey:#"data"]);
it gets logged as:
(
{
id = "14D19A9B-3D65-4FE2-9ACE-4C2D708DAAD8";
},
{
id = "8BFD10B8-F5FD-4CEE-A307-FE4382A0A7FD";
}
)
How can I parse the data as an NSDictionary with value and keys?
The web returns an object that has a property which is an array of objects, so...
NSDictionary *json= // your code
NSArray *array = json[#"data"];
for (NSDictionary *element in array) {
NSLog(#"%#", element);
// or, digging a little deeper
NSString *idString = element[#"id"];
NSLog(#"id=%#", idString);
}
Here's my code, when I run it, I get: "2014-10-26 19:02:09.153 App[27372:1281902] Price: (
)".
I was wondering why, no errors are being passed through and it honestly confuses me.
(I'm getting a blank response for "omc_usd_price")
#try
{
NSURL *url = [NSURL URLWithString:#"https://omnicha.in/api?method=getinfo"];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableDictionary *JSONStuff= [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSLog(#"%#",JSONStuff);
NSMutableArray * OMCArray = [[NSMutableArray alloc]init];
NSArray * responseArr = JSONStuff[#"omc_usd_price"];
for(NSDictionary * dict in responseArr)
{
[OMCArray addObject:[dict valueForKey:#"omc_usd_price"]];
}
NSLog(#"Price: %# test", OMCArray); // Here you get the Referance data
}
#catch (NSException *exception) {
NSLog(#"%#", exception);
}
#finally {
}
EDIT:
Tried this, I don't think I did this right either!
NSMutableArray * OMCArray = [[NSMutableArray alloc]init];
NSMutableArray * OMCArray2 = [[NSMutableArray alloc]init];
NSArray * responseArr = JSONStuff[#"response"];
NSArray * responseArr2 = JSONStuff[#"omc_usd_price"];
for(NSDictionary * dict in responseArr)
{
[OMCArray addObject:[dict valueForKey:#"response"]];
for(NSDictionary * dict2 in responseArr2)
{
[OMCArray addObject:[dict2 valueForKey:#"omc_usd_price"]];
}
}
NSLog(#"Price: %# test", OMCArray2); // Here you get the Referance data
Here is the JSONStuff dictionary:
{
"error":false,
"response":{
"block_count":96136,
"difficulty":12.18364177,
"netmhps":234.652099,
"seconds_since_block":694,
"avg_block_time":196.533,
"total_mined_omc":6426691.6,
"omc_btc_price":7.0e-6,
"omc_usd_price":0.0025,
"market_cap":15833.5909
}
}
As you can see, there is no entry in the dictionary named "omc_usd_price". There is a entry by that name in the dictionary name "response", but you didn't ask for that.
If you want to get omc_usd_price, as mentioned on your code, then you need to parse 2 dictionaries.
You must first parse the dictionary with the key response, and this will give you a new dictionary.
Then in this new dictionary you must parse/look for the key omc_usd_price.
Also, you're not getting an array back, but a double or float or even a string.
My application returns a NSMutableData *receivedData.
I've opted for using NSJSONSerialization to parse this under the assumption that it would be easiest. I'm having extreme trouble trying to get my head around how to do it. I'm very new to Objective-C, from a Java background.
In Java I used gson to parse the JSON in to an array which I could use easily. I'm really struggling with this here.
My current code for parsing the JSON is:
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(#"Error parsing JSON: %#", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(#"Item: %#", item);
}
}
As provided by somebody on the internet. This works and prints two items to NSLog. result and header. Here is how the JSON looks:
{
"header":{
"session":"sessionid",
"serviceVersion":"1",
"prefetchEnabled":true
},
"result":"50ce82401e826"
}
However if there is an error the JSON can also look like this:
{
"header":{
"session":"sessionid",
"serviceVersion":"1",
"prefetchEnabled":true
},
"fault":{
"code":0,
"message":"someErrorCode"
}
}
How I want the code to work:
Check if there is a "fault" object
If there is, print fault.code and fault.message to NSLog
If there isn't, I know that my JSON contains result instead of fault
Print the value of result to NSLog
But I can't for the life of me figure out how to approach it. Can someone please give me some pointers?
your object appears to be a dictionary.
Try this out.
NSError *e = nil;
id jsonObj = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
NSArray *jsonArray = nil;
NSDictionary *jsonDict = nil;
if ([jsonObj isKindOfClass:[NSArray class]]){
jsonArray = (NSArray*)jsonObj;
}
else if ([jsonObj isKindOfClass:[NSDictionary class]]){
jsonDict = (NSDictionary*)jsonObj;
}
if (jsonArray != nil) {
// you have an array;
for(NSDictionary *item in jsonArray) {
NSLog(#"Item: %#", item);
}
}
else if (jsonDict != nil){
for (NSString *key in jsonDict.allKeys){
NSLog(#"Key: %# forItem: %#",key,[jsonDict valueForKey:key]);
}
}
else {
NSLog(#"Error: %#",e);
}
when i have the english character in my json file it's printing the read json in array, but when i have tibetan character(utf8) it's printing null. i am pasting my json and xcode code. please help. this code is what i am trying from core data tutorial
int main(int argc, const char * argv[])
{
#autoreleasepool {
// Create the managed object context
NSManagedObjectContext *context = managedObjectContext();
// Custom code here...
// Save the managed object context
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Error while saving %#", ([error localizedDescription] != nil) ? [error localizedDescription] : #"Unknown Error");
exit(1);
}
NSError* err = nil;
NSString* dataPath = [[NSBundle mainBundle] pathForResource:#"tsikzoe" ofType:#"json"];
NSDictionary* tsikzoe = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]options:kNilOptions error:&err];
NSLog(#"Imported tsikzoe: %#",[tsikzoe objectForKey:#"wordDef"]);
}
return 0;
}
Here is the JSON
[{"id":1,"dictionaryType":1,"word":"༼༡༠༠༦༽ སྲོང་བཙན་ཆོས་ཀྱི་རྗེ་བོ་ཡབ་ཡུམ་གསུམ།","wordDef":"རྒྱལ་པོ་སྲུང་བཙན་སྒམ་པོ། བལ་བཟའ་ཁྲི་བཙུན། རྒྱ་བཟའ་ཀོང་ཇོ་བཅས་གསུམ་ལ་ཟེར།","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"དུང་དཀར་ཚིག་མཛོད།"},
{"id":2,"dictionaryType":1,"word":"༼༡༠༤༠༽ བསོ།","wordDef":"བསོ་བསོ་ཞེས་པའི་སྒྲའི་ཁྱད་པར་གྱི་མིང༌།","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"དུང་དཀར་ཚིག་མཛོད།"}]
NSDictionary* tsikzoe = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]options:kNilOptions error:&err];
Your JASON is an array in this case, so you will need to access one of the two elements in this array, which would be a dictionary.
A dictionary JSON will have this format
{
"employees": [ //right here!
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
An Array JSON will have this format
{
[ //notice the difference.
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
Trying to parse this JSON object in objective-C and creating an NSArray with these objects.
The first value is a counter and is specific for the object. All other values are unique.
{ "myData": [
["1","1","110","dollar","8.0","2.8","0.1","11.6"],
["2","1","110","euro","4.0","3.2","1.5","4.4"],
["3","1","120","rupier","6.0","2.9","1.3","10.8"],
["4","1","120","dinero","4.0","3.3","1.5","4.4"],
["5","2","130","drahmer","8.0","2.9","1.3","11.2"],
] }
Tried this code:
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:myData
options:kNilOptions
error:&error];
NSArray *currencyInformation = [json objectForKey:#"myData"];
But the objects are not there. Though the count of the array is 5.
Each object in the array is an array itself, so:
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:myData
options:kNilOptions
error:&error];
NSArray *currencyInformation = [json objectForKey:#"myData"];
for (NSArray *info in currencyInformation) {
// Then access each "column" with [info objectAtIndex:0,1,2,3,...]
}
In this data structure you would need to access things by index e.g
for (NSArray *currency in currencyInformation) {
NSLog(#"Currency: %#", [currency objectAtIndex:3]);
}
If you want to access things by key then you would need to change your JSON to use an array of objects instead of an array of arrays. Something like this:
{
"myData": [
{
"primaryKey" : 1,
"currency" : "dollar",
<other keys + values>...
},
]
}
In which case you could now do something like:
for (NSDictionary *currency in currencyInformation) {
NSLog(#"Currency: %#", [currency valueForKey:#"currency"]);
}