I am trying to parse a rather large json file, and I am beginning to have a bit of an issue
An example of what the JSON looks like is this
{
"info":"info",
"products":[
{
"name":"HomeScreen",
"screens":[
{
"name":"View Details",
"id":1,
"title":{
"name":"Default_Name",
"number":"Default_Number"
},
"widgets":[
{
"id":8,
"splits":[
{
"control":{
"type":1,
"limitLow":0,
"limitHigh":100
},
"texts":[
{
"value":"blah",
"style":{
"size":10,
"colour":"#FFFFFF"
},
"test1":"asd",
"test2":"zxc"
}
]
}
]
},
And so on
I want to be able to parse the texts values and place them in an object. I have the class created, but I am unable to retrieve the exact values. I have tried doing the following
NSArray *test = [MainJSON valueForKeyPath:@"products.screens.widgets.splits.texts"];
for(NSDictionary *dict in test){
NSLog(@"%@", [dict valueForKeyPath:@"value"]);
}
It prints out the values but with loads of brackets as well, and I need to be able to get the exact value from the JSON. I also tried doing
for(int i = 0; i < [test count]; i++){
for(NSDictionary *dict in [test objectAtIndex:i]){
NSLog(@"%@", [dict valueForKeyPath:@"value"]);
}
}
But same problem. Could anyone tell me what I am doing wrong. I have tried soo many different solutions, but nothing seems to be working.
Thanks in advance
EDIT Output for example I gave is
2012-03-21 12:12:18.731 JSONObjects[4467:f803] (
(
(
(
"CURRENT BALANCE",
"[var:UnbilledAmount]"
)
),
(
(
"THE BILL IS DUE IN",
"[var:DaysToBill] DAYS"
)
),
(
(
TEXTS,
"[var:UsedTexts] of [var:MaxTexts]"
),
(
DATA,
"[var:UsedData] of [var:MaxData]"
),
(
TEXTS,
"[var:UsedMinutes] of [var:MaxMinutes]"
)
)
)
)
2012-03-21 12:12:18.732 JSONObjects[4467:f803] (
(
),
(
),
(
),
(
),
(
)
)
2012-03-21 12:12:18.732 JSONObjects[4467:f803] (
(
)
)
2012-03-21 12:12:18.733 JSONObjects[4467:f803] (
(
)
)
2012-03-21 12:12:18.733 JSONObjects[4467:f803] (
(
)
)
2012-03-21 12:12:18.734 JSONObjects[4467:f803] (
(
)
)
2012-03-21 12:12:18.734 JSONObjects[4467:f803] (
(
)
)
As you can see a lot of brackets which is deeply frustrating.
Edit2:
Test is an array of NSDictionary objects though, here is the code
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"json"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
NSDictionary *MainJSON = [responseString JSONValue];
NSArray *test = [MainJSON valueForKeyPath:@"products.screens.widgets.splits.texts"];
Edit:
I have been able to get a single value using this code
for(NSDictionary *dict in [textArray objectAtIndex:0]){
NSLog(@"%@", [[[[dict valueForKey:@"value"]objectAtIndex:0]objectAtIndex:0]objectAtIndex:0]);
}
However this is a bit messy.
I figured it out in the end. In the products Object class, I didn’t add in an NSDictionary object into the class object. As I needed to make a dictionary of the screens. Then loop through that dictionary, find the widgets, then create a dictionary of that array, find the splits, then make a dictionary of that array, and then loop through that and so on until I got to texts. Now it works perfectly! 😀